dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: really dumb date problem

  1. #1
    Join Date
    Jul 2010
    Location
    Birmingham, UK
    Posts
    67

    really dumb date problem

    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.

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    The only dumb question is the one un-asked.

    If you are using PHP 5 and you know your user's time zone then see http://www.php.net/manual/en/functio...mezone-set.php . If you don't then you can use JavaScript. It won't work for a very large number of users but you can design around that so that it fails gracefully. But then your users already have the local time on screen, down on the lower, right corner. Double posting the time is more annoying than useful.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Jul 2010
    Location
    Birmingham, UK
    Posts
    67
    I suppose what I was asking - I'm sorry, I just thought I was clear there - is how to get my users' posts to read "Saturday, 06 August 2011 11:53:17 EDT" if they are in New York or whatever instead of "Saturday, 06 August 2011 08:53:17 PDT" where the server is located. I am really trying to avoid using Javascript in this case, but I just sort of thought that if I can get a user's IP and location using only PHP then perhaps there is a date function that can give me a correctly formatted date w/o jumping through a lot of hoops.

    This particular PHP file is running with PHP 4, so date_timezone_set doesn't seem like an option. The other problem with using Javascript, it seems, is that whenever I need to convert the date to another format (which happens now and again) I have to needlessly move over from PHP to Javascript and back again to avoid the same problem of the server's timezone getting used. Which is really, really messy.

  4. #4
    Join Date
    Mar 2011
    Location
    Ponca City, OK
    Posts
    42
    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.

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    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.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Jul 2010
    Location
    Birmingham, UK
    Posts
    67

    resolved

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles