What can use for DateTime::diff() for PHP 5.2?

后端 未结 10 1675
情歌与酒
情歌与酒 2020-11-27 06:18

Is there any function equivalent to DateTime::diff() in PHP 5.2?

My local server is PHP 5.3 and using DateTime::diff(). then I found that my live site uses PHP 5.2 a

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 06:56

    I use this, seems to work alright - obviously you can add a second parameter to make it more flexible:

    function GetDateDiffFromNow($originalDate) 
    {
        $unixOriginalDate = strtotime($originalDate);
        $unixNowDate = strtotime('now');
        $difference = $unixNowDate - $unixOriginalDate ;
        $days = (int)($difference / 86400);
        $hours = (int)($difference / 3600);
        $minutes = (int)($difference / 60);
        $seconds = $difference;
    
        // now do what you want with this now and return ...
    }
    

提交回复
热议问题