Anonymous
Anonymous

Reputation: 3689

php DateTime object with unix timestamp wrong - bug?

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

Upvotes: 1

Views: 1221

Answers (1)

zerkms
zerkms

Reputation: 254956

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');

Upvotes: 2

Related Questions