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

后端 未结 10 1716
情歌与酒
情歌与酒 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:50

    Spudley's answer doesn't work for me--subtracting any DateTime from another gives 0 on my system.

    I was able to get it to work by using DateTime::format with the 'U' specifier (seconds since Unix epoch):

    $start = new DateTime('2010-10-12');
    $end = new DateTime();
    $days = round(($end->format('U') - $start->format('U')) / (60*60*24));
    

    This works on both my dev system (5.3.4) and my deployment system (5.2.11).

提交回复
热议问题