Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Just stumbled upon this weird bug with php's DateTime object... Check this out:

<?php
$date = 1335823200;
echo date('d',$date);
echo '<br />';
$date = new DateTime("@$date");
echo $date->format('d');
?>

Returns:

06
05

It doesn't happen with any timestamp. I suspect that it has something to do with different timezones, but playing around with setlocale() didn't help anything. By the way, the '@' in the DateTime is needed to be able to use unix timestamps (see bug report here). Here a few more timestamps to test:

1333663200
1338588000
1338847200
share|improve this question

1 Answer

up vote 2 down vote accepted

Since you did not specify timezone for DateTime it is supposed that it is UTC, while date respects current timezone (specified by date_default_timezone_set or taken from php.ini). Just execute this and see:

$date = 1335823200;
echo date('d-m-Y  H:i:s',$date);
echo '<br />';
$date = new DateTime("@$date");
echo $date->format('d-m-Y H:i:s');
share|improve this answer
Damn, I thought it would both just use the settings from the same source. Do you know if I can globally define a default for DateTime (before instanciating)? – Anonymous Oct 7 '11 at 2:48
@danontheline: manual says that php should use default timezone, but actually on my php 5.2 it is not correct. Probably it has been fixed in 5.3 – zerkms Oct 7 '11 at 2:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.