I recently switched over to a new server, presumably in a completely different time zone from the last, and I am getting new users in different parts of the world now - both of which are leading me to discover this dumb new problem with my old date/time formats.
Basically, and I'm sure I'm not the first to ask this, I'm trying to get the current time put into a format that reflects the user's local time - not my server's local time. My code as it is now looks like this: date('l, d F Y H:i:s T'), and the format itself is correct. The problem, of course, is that it is showing the time in PDT where the server is. Adding or subtracting from the timestamp only reflects the incorrect time in PDT and is not ideal anyway, and it seems that there must be a less messy way of giving the users local time using a straight-forward, standard PHP date/time function.
I have looked through a list of the various functions in the PHP resources, but without fail I just get variations to the time in PDT. So I'm sort of scratching my head here a bit.
08-06-2011, 03:10 PM
The only dumb question is the one un-asked.
08-06-2011, 04:26 PM
08-06-2011, 04:56 PM
The best method in my opinion, and the one that I use is this:
First all dates and times on my site are stored as a unix timestamp. Better then storing August 1st 2011 13:45 in the database and offers the most flexibility in displaying and manipulating dates.
Second, on the server all times are stored based on the servers timezone. I make a note on my site that says what the default timezone is. I allow my users to select their timezone in their profile, which updates all the dates on the site to allow for their timezone offset. So all the guests see the server time and the logged in users see all the times based on their timezone. This allows for showing dates/times like, Yesterday, Today, x minutes ago, x hours ago, x days ago, all correctly based on the users current time.
Hope this post helps you out.
08-06-2011, 07:36 PM
I'm thinking that you're just going to have to upgrade to PHP 5. Timezone rules are far more complicated than you realize, perhaps.
08-08-2011, 03:02 PM
Thanks to everyone for the input. I have a managed to solve the problem by creating a function that feeds the user's location to an outside api which in turn sends back the user's local time. I would normally want to keep it simple and work only with timestamps, but in this case it was important to keep the time in a format which included "PDT" or "EDT" or whatever... so that later on the time could be manipulated for other purposes to display the user's time of writing a message, etc. As I say, the problem with just storing away a timestamp is that when it is converted to a user-readable format it reflects the time where the server is, which just confuses people.
Being stuck with php 4, in this case, sort of tied my hands a bit, but with the new function is place it all seems to work well - and it's all packaged up into a tidy function that limits the fuss. I'm still sort of amazed that this weird limitation exists, but it looks like the options in php 5 are worth exploring in the future if I'm going to have users in different parts of the world.