简体   繁体   中英

How to calculate date and time difference?

Can somebody tell if my below code is correct.I calculate difference between date requested and current time.But I'm getting the $timediff value in negative like this: -28200

    $dateRequest =  "2015-08-21 15:16:02";
    date_default_timezone_set("Asia/Kuala_Lumpur");
    echo "now ".$now=date('Y-m-d, g:i A',time());
    $timezone_offset = +8;
    echo "then ".$then=date('Y-m-d, g:i A', strtotime($dateRequest)+$timezone_offset*60*60);

    echo "<<".$timediff = strtotime($now) - strtotime($then);// in seconds
     if($timediff > 120)// 120  is 2 mins
    {
        echo "Expired Link";
    }

In your example

$then date is always in the future by 8 hours (8*60*60) = 28800

It's just basic maths

$now - ($then + 28800) = -28800

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM