MZON
MZON

Reputation: 305

Get different results by using date and DateTime

I can't undestand why do I get a different results by using date() function and DateTime object. I'm on Mac.

date_default_timezone_set('Europe/Sofia');

echo date('Y-m-d h:i:s'); // 2015-04-02 01:18:59 correct

$date = new DateTime('@'.time());  
echo $date->format('Y-m-d h:i:s');  // 2015-04-01 10:18:59 offset

Edit

Tried $date = new DateTime('@'.time(), new DateTimeZone('Europe/Sofia')); no effect

Upvotes: 0

Views: 120

Answers (1)

Rizier123
Rizier123

Reputation: 59681

date_default_timezone_set() doesn't effect the DateTime class so you have to set it with the methods from DateTime like this:

$date = new DateTime("@".time());  
$date->setTimezone(new DateTimeZone('Europe/Sofia'));
echo $date->format('Y-m-d h:i:s') . "<br>";  

Side Note:

Normally you could also do this:

$date = new DateTime("@".time(), new DateTimeZone('Europe/Sofia')); 

But since you use a timestamp this is not possible for some reason.

This is already in the bug tracker here: https://bugs.php.net/bug.php?id=40743

Upvotes: 2

Related Questions