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

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

    Please observe that if your DateTime object was created from a date string without any timezone information (as in '2012-01-01 05:00:00' like from mysql), then setting the timezone later with DateTimeZone objects via setTimeZone() does not change the DateTime objects internal timestamp.

    $localtime = new DateTime('2012-01-01 08:00:00+02:00'); // Europe/Copenhagen (+ daylight savings)
    $localtime->setTimezone(new DateTimeZone('UTC')); // convert local time to utc
    
    $utctime = new DateTime('2012-01-01 06:00:00'); // timezone assumed utc, but is in fact unknown
    $utctime->setTimezone(new DateTimeZone('UTC')); // trying to rectify missing timezone which fails, because the internal timestamp isn't modified, although any other format than 'U' may indicate so
    #$utctime = new DateTime('2012-01-01 06:00:00+00:00'); // timezone stated, this works
    
    $diff = intval($localtime->format('U')) - intval($utctime->format('U'));
    echo $diff; // expecting zero
    

提交回复
热议问题