开发者

DateTime->diff() crashes

I have a few lines of PHP that should work, but don't:

$date1=new DateTime();
$date2=new DateTime();

$interval=date_diff($date1,$date2);

or

$interval=$date1->diff($date2);

I even copied the example from the DateTime Docs:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

The output is blank, when I debug, the debugger disconnects at the diff line without any error message. Is there any known bug in PHP Version 5.2.9 or has anyone else seen this behaviour?

I am currently out of ideas on what to try except for 开发者_运维问答updating the PHP version. But updating all customers is not currently something I want to do...

Every little helps... Thanks!


DateTime:Diff() is PHP >= 5.3.0 only.

Not sure why your debugger bails out completely - it should be complaining about an undeclared method. Maybe you need to adjust your error reporting settings?


Seems pretty right to me. Could you try

date_default_timezone_set('Europe/London');
$date1 = new DateTime('2009-10-11');
$date2 = new DateTime('2009-10-13');

$time1 = $date1->format('Y-m-d');
$time2 = $date2->format('Y-m-d');

$difference = abs(strtotime($time1)-strtotime($time2));

$difference/= 3600*24;

print $difference.' days';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜