Forum Moderators: coopster

Message Too Old, No Replies

Store dates as GMT, display as local date/time?

         

JAB Creations

11:39 am on Sep 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm having trouble figuring out how to display date/time as the local time zone (let's say -0500) from GMT database stored date/time stamps.

The main thing I've tried was setting MySQL's time zone setting to GMT for POST requests and using the local time zone for GET requests...

POST Request / MySQL
SET time_zone = 'GMT'


GET Request / MySQL
SET time_zone = 'America/New_York'


I'm also doing the same for PHP...

if (!isset($_SESSION['time_zone']) || $_SERVER['REQUEST_METHOD']=='POST') {date_default_timezone_set('Europe/London');}
else {date_default_timezone_set($_SESSION['time_zone']);}


Thoughts please?

- John

JAB Creations

12:14 pm on Sep 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I came across a post else where on the internet mentioning the use of strtotime which is cool as I use it to format dates from SQL's datetime time stamp.

However it seems to work backwards.

This returns 5pm for a post in my software set as GMT 12pm which I made at 8AM -0500 (Eastern Standard Time). So it's stored in MySQL correctly as GMT time but it's doing the reverse number of hours for the time zone...I think. The time in the database is stored in this format: 2010-09-09 12:03:08.
function user_date_mysql($date,$format) {return date ($format,strtotime($date.'America/New_York'));}


The same thing happens if I use -0500 or +0500 as EST is -0500 and so using +0500 would be for somewhere in East Europe I think.

So I am thinking I may want to always use GMT time for MySQL's time zone and then set the local time zone via PHP's date_default_timezone_set and use strtotime however I would really appreciate some clarification so I can move forward with this. :)

- John

JAB Creations

11:35 am on Sep 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I figured it out! Turns out I was doing the opposite of what I should have been doing.

So I made a post on my forums a little after 7am here in the -500 time zone and the date is stored as GMT (presume it's coming from a NOW() date/time stamp in SQL). The following code works correctly... :)

- John

<?php
date_default_timezone_set('America/New_York');
function user_date_mysql($date,$format) {return date ($format,strtotime($date.'GMT'));}

$date = '2010-09-10 11:13:49';

echo user_date_mysql($date,'l M. jS, Y, h:iA');
?>