Thought I would follow up with some weirdness that I have found after
using the DateTime object a bit.
Consider the following code:
<?php
$date_time = new DateTime('2010-05-26 10:00:00', new
DateTimeZone('America/Los_Angeles'));
echo 'Before timezone conversion:' . PHP_EOL;
echo $date_time->format('U') . PHP_EOL;
echo $date_time->format('r') . PHP_EOL;
echo strtotime($date_time->format('Y-m-d H:i:s')) . PHP_EOL;
$date_time->setTimezone(new DateTimeZone('America/New_York'));
echo 'After timezone conversion:' . PHP_EOL;
echo $date_time->format('U') . PHP_EOL;
echo $date_time->format('r') . PHP_EOL;
echo strtotime($date_time->format('Y-m-d H:i:s')) . PHP_EOL;
?>
The format function takes any argument that the regular date()
function call will. So 'U' is seconds since unix epoch. I would expect
that the the two format('U') function calls would produce different
results since we have changed the timezone and the format('r')
reflects that. However this is not the case, they return the exact
same timestamp - 1274893200. This appears to convert whatever
datetime you have stored to your default timezone before returning the
timestamp. This really stumped me for a bit, so thought I would
mention it here.
I got around this by using the strtotime function on a format from
DateTime that did not include the timezone(difference from Greenwich
time). See last line before and after timezone conversion. Which seems
to work, but there must be a better way.
Do any of you with more DateTime experience know of a better way to
handle this with functions that work in PHP < 5.3?
Thanks,
tm
I have just *never* had to deal with timezones so never had to think about it.
I guess in my case it would make more sense to convert the date from
whatever timezone I am bringing it in from to the local servers time,
then store to database. Then convert back to appropriate timezone when
sending out. Rather then storing a unix timestamp.
Thanks Again,
tm
I quickly came to the conclusion that converting/storing to GMT seems to
be the most consistent and safest approach. And then when you retrieve
it, transform it to the timezone you need. Alternatively, you could
store the value in *any* timezone as long as you note which one(s) it
is, but it needs to be documented *somewhere*.
My 0.02,
keith
--
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
D. Keith Casey, Jr.
Chief Technology Officer
http://BlueParabola.com
Good point.
>
> I quickly came to the conclusion that converting/storing to GMT seems to be
> the most consistent and safest approach. And then when you retrieve it,
> transform it to the timezone you need. Alternatively, you could store the
> value in *any* timezone as long as you note which one(s) it is, but it needs
> to be documented *somewhere*.
All of our data goes out in Eastern(EST/EDT) as a standard and is
documented as such. So in this case I think I am ok. Though your point
above does make good sense if you don't have a standard, or it might
change.
-tm
>
> My 0.02,
> keith
>
> --
> =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
> D. Keith Casey, Jr.
> Chief Technology Officer
> http://BlueParabola.com
>