Link to home
Start Free TrialLog in
Avatar of Ryan Bayne
Ryan BayneFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding days to a date and comparing dates within argument

Hi

I need to check the datetime I have in my DB (MSSQL) against the current datetime. If the current datetime is 7 days after the DB stored datetime then true.

            if($now >= $wcpastdate){
}
else { current datetime is not 7 days after the DB datetime so do something different here }

So I was thinking I would have to increase the stored date by 7 and if its still more than the current date I dont have to do anything otherwise I need to reset the datetime in database.

Thanks for any solutions
Avatar of Cem Türk
Cem Türk
Flag of Türkiye image

you can check results of the following query :)

SELECT TOP 1 DATEADD(day, 7, yourdatetimefield) AS dbdate from yourtable where dbdate>GETDATE()
ASKER CERTIFIED SOLUTION
Avatar of nplib
nplib
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ryan Bayne

ASKER

  Nice, but for my use I think it would be minus.

So basically take 7 away from the current days ($now) and if it is still AFTER the past date retrieved from the datebase ($wcpastdate) then the past date is older than 7 days and needs to be replaced else if ($now) ends up a date before that date than it skips the first part of if statement.

 $now = strtotime('-7 days', date('d-m-y'));
    //var_dump($now, $wcpastdate); exit;
           
    //Check if $now is 6 days 23 hours and 59 minutes more than datetime in weeklycar table
    if($now >= strtotime($wcpastdate)){
            $sql="DELETE FROM weeklycar WHERE stockID = '$wcpastid'";
            $deleterow=mssql_query($sql);
            if (!$deleterow) {die('Query execution problem delete the current car of the week');}

Thats great thanks
Aw got an error

A non well formed numeric value encountered in C:\AppServ\www\cars\inc\weeklycar.php on line 16

    $now = strtotime('-7 days', date('d-m-y'));


sorry my mistake, it needs to be a unix date stamp.

$now = strtotime('-7 days', date('U'));
Thanks