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

后端 未结 10 1690
情歌与酒
情歌与酒 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 07:01

    PHP has methods for working with Unix timestamps.

    As has been noted by others, by working with seconds since the Unix date, it is easy to calculate times.

    PHP's strtotime() converts a date to a timestamp:

    $diff = round((strtotime($list['start']) - strtotime($list['finish'])) / 86400);
    

    If you wish to calculate till the current time, time() provides the timestamp of "now":

    $diff = round((time() - strtotime($list['date'])) / 86400);
    

    86400 is the number of seconds in a day.
    If you wish to convert to years use 31557000, which is almost exactly 365.24219 * 86400.

    An added advantage here is that strtotime can take the input date in almost any human readable format, so it is very easy to work with within the code.

提交回复
热议问题