Reputation: 4849
20:30
UK time a user from the Netherlands should see the start time as 21:30
.DateTime
objects to set my events to. However, when I specify my DateTimeZone
object it shows +00:00
in stead of the DateTimeZone
I have created for it. This means that the new times are incorrect. So time that were set to 20:30
UK time still roll out as 20:30
NL time in stead of 21:30
.
DateTimeZone
is still incorrect when assigned to the DateTime
object.
The default setting:
date_default_timezone_set('Europe/London');
The code that should convert DateTime
objects to local time:
$user_timezone = Helper::get_user_timezone(); //contains "Europe/Amsterdam"
foreach($events as $event){
$timestamp = $event->getDatetime()->getTimestamp();
$local_datetime = new DateTime('@'.$timestamp, new DateTimeZone($user_timezone));
}
DateTime
object as input:
DateTime Object
(
[date] => 2015-02-14 19:30:00.000000
[timezone_type] => 3
[timezone] => Europe/London
)
DateTime
object for me:
DateTime Object
(
[date] => 2015-02-14 19:30:00.000000
[timezone_type] => 1
[timezone] => +00:00
)
DateTimeZone
.
DateTimeZone
before the creation of the DateTime
object. I made sure it was a correct object and set it in the DateTime
constructor then.DateTime
object first with just a timestamp as parameter, and setting the DateTimeZone
object afterwards (I know there is/was a bug that caused some weird behaviour with setting DateTimeZone
directly). Europe/Amsterdam
) is legit according to the docs. I'm starting to wonder if it's not possible to convert the date when using a timestamp
?
I have also looked at these question for the basic gist, but they have no such problem:
1. Converting between timezones in PHP
2. How to convert between time zones in PHP using the DateTime class?
I followed a couple more, but I can't find them at the moment.
DateTime
object getting an incorrect DateTimeZone
?
DateTimeZone
objects are correct I get the following:
DateTimeZone Object
(
[timezone_type] => 3
[timezone] => Europe/Amsterdam
)
But when I set it to the new DateTime
object both the timezone_type
and timezone
change to:
DateTime Object
(
[date] => 2015-02-14 19:30:00.000000
[timezone_type] => 1
[timezone] => +00:00
)
You'll notice the different timezone_type
and timezone
.
Upvotes: 4
Views: 5699
Reputation: 643
As per documentation:
Note:
The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
So, just use the "setTimezone()" solution or provide a datetime string :)
Upvotes: 0
Reputation: 72177
Use method DateTime::setTimeZone()
:
date_default_timezone_set('Europe/London');
$date = new DateTime('2015-02-14 19:30:00');
print_r($date);
echo("Local time in London: ".$date->format('Y-m-d H:i:s')."\n");
$date->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
print_r($date);
echo("Local time in Amsterdam: ".$date->format('Y-m-d H:i:s')."\n");
The output of the script above:
DateTime Object
(
[date] => 2015-02-14 19:30:00.000000
[timezone_type] => 3
[timezone] => Europe/London
)
Local time in London: 2015-02-14 19:30:00
DateTime Object
(
[date] => 2015-02-14 20:30:00.000000
[timezone_type] => 3
[timezone] => Europe/Amsterdam
)
Local time in Amsterdam: 2015-02-14 20:30:00
If you don't want to modify the original $date
object then you can clone
it and modify the timezone of the clone:
$roDate = clone $date;
$roDate->setTimeZone(new DateTimeZone('Europe/Bucharest'));
echo("Local time in Bucharest: ".$roDate->format('Y-m-d H:i:s')."\n");
displays:
Local time in Bucharest: 2015-02-14 21:30:00
I ran the code with PHP 5.6.6 on OSX.
The DateTime
and related classes had a lot of bugs in their early versions (PHP 5.2, PHP 5.3). Most of them were fixed in the recent versions (but there still are some well hidden minor issues).
My suggestion is to use the most recent version of PHP you can install on your system.
Upvotes: 6