Reputation: 3571
The problem is really simple, but don't know why things are not working in a simple way.
I want to have datetime of specific timezones. My server's timezone is America/Chicago
but I want to get current datetime of different timezones.
date_default_timezone_set
as it changes timezone for all datetime functions.$now = new DateTime(null, new DateTimeZone('Europe/Stockholm'));
with different timezone values but all returns the same value.Consider the following code
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
$now = new DateTime(null, new DateTimeZone('America/New_York'));
echo "America/New_York = ". $now->getTimestamp() . "\r\n";
$now = new DateTime(null, new DateTimeZone('Europe/Stockholm'));
echo "Europe/Stockholm = ". $now->getTimestamp() . "\r\n";
$now = new DateTime(null, new DateTimeZone('Asia/Muscat'));
echo "Asia/Muscat = ".$now->getTimestamp() . "\r\n";
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
The above code results in following output
The current server time is: 2015-04-04 17:06:01
America/New_York = 1428185161
Europe/Stockholm = 1428185161
Asia/Muscat = 1428185161
The current server time is: 2015-04-04 17:06:01
And all three values are same, means new DateTimeZone(XYZ)
didn't work. The expected/required output should echo current time of those specific timezones.
Please advice if I'm missing anything.
Upvotes: 2
Views: 3156
Reputation: 219824
Unix Timestamps are always in UTC and therefore always the same. Try using the c
formatter to see the differences:
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
$now = new DateTime(null, new DateTimeZone('America/New_York'));
echo "America/New_York = ". $now->format('c') . "\r\n";
$now = new DateTime(null, new DateTimeZone('Europe/Stockholm'));
echo "Europe/Stockholm = ". $now->format('c') . "\r\n";
$now = new DateTime(null, new DateTimeZone('Asia/Muscat'));
echo "Asia/Muscat = ".$now->format('c') . "\r\n";
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
The current server time is: 2015-04-04 22:26:56
America/New_York = 2015-04-04T18:26:56-04:00
Europe/Stockholm = 2015-04-05T00:26:56+02:00
Asia/Muscat = 2015-04-05T02:26:56+04:00
The current server time is: 2015-04-04 22:26:56
Upvotes: 4