1

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

1 Answer 1

2

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');
2
  • 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)? Commented Oct 7, 2011 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 Commented Oct 7, 2011 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.