Re: The Big Date and Time Debacle

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Christopher Taylor wrote:
Stephen Martindale wrote:
I am having great difficulty understanding dates and times with PHP and MySql.

As far as I understand them, the PHP date and time construct is timezone and DST aware, but MySql's DATETIME and TIMESTAMP fields are not. I believe that this is where my confusion originates from.

After searching the web and the docs for hours, I have not managed to find an article that explains how this system works and what the best practices are for an application that may be used in many timezones, some with DST and some without.

Please point me in the direction of a good source on this subject. I am new to PHP, coming from a several-year-long period of C++ and, recently, ASP.NET. (Ok, I admit it, I only started using PHP a week ago!)

BTW: I've loved every minute of it! PHP Rocks!


Stephen,

I don't know what is considered standard, but for the apps that I have worked on, including php and c++, timestamps are always stored in GMT and presented in the local timezone. In the case of the database, the timestamp is GMT and another field is used to store the timezone offset. The timezone offset changes in regards to DST. For example NYC is -5 hours part of the year and -4 hours. Hope this helps.

Chris

Hi Chris, thanks for the reply.

I assumed that I would have to store the time in the database as GMT. I wrote a date and time wrapper class that could easily convert any UNIX timestamp to a MySql compatible GMT string using:

gmdate("Y-m-d H:i:s", $this->_dateTime);

I would convert from the MySql string back to a timestamp with:
strtotime($dateString . " GMT")

(where $dateString is the value that comes from MySql)

Is this safe and correct?

If I was capturing user input, assuming I new the timezone that the user was in and I knew whether to automatically adjust for DST or not (timezones like mine don't use GMT) how would I convert from the user input into a UNIX timestamp that I could then gmdate() to insert into MySql?

Once I had retrieved a date from MySql and converted it into a UNIX timestamp in PHP, I can display it on the screen using
date(DATE_W3C, $this->_dateTime);
This would show a string with the timezone from my PHP environment, how could I modify this line to display it in a timezone depending on my user's profile?

Am I correct in assuming that there is no reliable method of retrieving the time offset of a user from the HTTP request? That would make my life a lot easier!

Thanks for any help,
Stephen

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux