Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert stored mysql DateTime of UTC datetime stamp into another time zone

12 views
Skip to first unread message

Justin Voelker

unread,
Dec 2, 2008, 2:48:30 PM12/2/08
to
Hello everyone:
I have seen posts like this one a couple times looking through this
group but I have yet to find a simple, elegant answer and I know there
must be one. I have a mysql database that has a datetime field in the
format Y-m-d H:i:s for the UTC time of a given post. If I am in the
eastern timezone I am currently at -5 hours from UTC so when I make my
post at 1:00pm eastern time, the time I see dispalyed on the page is
6:00pm because it is 6:00pm UTC. How can I take that UTC datetime
stamp and convert it to the correct datetime given a specific php
timezone such as America/New_York? I know there must be a simple way
to do this I just haven't been searching the right words or
something. Please help! Thank you!!!

Jerry Stuckle

unread,
Dec 2, 2008, 3:54:27 PM12/2/08
to

Among other ways:

echo strftime('%Y-%m-%d %H:%M:%S', strtotime('2008-12-02 20:50:00Z'));

or

echo date('Y-m-d H:i:s', strtotime('2008-12-02 20:50:00Z'));

prints:

2008-12-02 15:50:00

(U.S. Eastern Standard Time)

(Note: the 'Z' at the end of the string is important - it means the
input is in Zulu (UTC) time).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Justin Voelker

unread,
Dec 4, 2008, 3:19:07 PM12/4/08
to
> jstuck...@attglobal.net
> ==================

Thank you Jerry:
I think that would work if the user selects a timezone and I set that
timezone on the website using date_default_timezone_set(). The
problem is, when I need to write a date/time back to the database, I
need to set the timezone back to UTC. Is there anything I can do to
convert on the fly rather than constantly switching back and forth
with the date_default_timezone_set() function?

Jerry Stuckle

unread,
Dec 4, 2008, 4:22:12 PM12/4/08
to

php.net is your friend.

http://www.php.net/manual/en/function.gmstrftime.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

jstu...@attglobal.net
==================

0 new messages