Adjusting the date with php

Will asked about adjusting the date php displays to account for the fact that the server may not be in the same location as you. (I don’t think this solved his problem with displaying the actual timezone differently - but thought it was worth putting up anyway…)

I’m sure there are other ways (if you got ‘em feel free to post them or links to them in the comments. It’s always interesting to see how different people solve the same problem) :)
Here’s what I do:

Define a variable (in this example I’m calling it $timedifference) as the number of hours you want to add/subtract x 3600 (the timestamp value of an hour). If you’re subtracting put a - before the number.

So lets say we want to show the time two hours behind what the server is displaing. (3600×2=7200) So we define timedifference like this:

$timedifference = -7200;

Or lets say we want to display the time three hours ahead. (3600×3=10800):

$timedifference = 10800;

Now lets set our “starting point time”. Sometimes this might be a timestamp stored in the database. So you’d define it like:

$thetime = $row['timestamp'];

or maybe you just want to display the current time:

$thetime=time();

Now, to display the date like: January 20, 2003 3:02 pm (see date() function for more details on different displays), you can do it like this:

date(”F j, Y, g:i a”,$thetime+$timedifference)

here’s a page I created so you can see/play with that a bit

6 Responses to “Adjusting the date with php”

  1. Jennifer Says:

    Doing some more work on this… to SET the timezone (and the appropriate time for that timezone) without having to do the addition, etc., it seems like you can just do this:

    putenv(”TZ=PST8EDT”);

    However, I can’t find at the moment what TZ can equal, though…

  2. Will Says:

    Here’s a list of valid TZ values:

    http://www.bsdi.com/xdate

    If one of those doesn’t work for you, and you have shell access to your account, try having a look at /usr/share/zoneinfo/ or /usr/lib/zoneinfo/ to see what’s available to you.

  3. kristine Says:

    Here’s what I tend to use, as it doesn’t require multiplying ;)

    date(”Ymd”, strtotime(”-3 hours”))

    I’m gonna take a look at the TZ stuff though!! Thanks :)

  4. Jennifer Says:

    I had some inconsistencies using strtotime and I think it was especially with “-” numbers or it had to do with using a set timestamp (like from a timestamp in a database)

    but I never made a note of what it was… :-\ (That’s why i started using the mulitiplication stuff)

  5. Jennifer Says:

    Of course, I’m trying it now (because it’s going to drive me crazy that i can’t remember what it was) and it’s all working fine… oh well… when I figure it out, I guess I better remember to post it here! LOL!

  6. Bill Kearney Says:

    Do not play with timestamps using multipliers. Mainly because of timezone shifts for daylight savings switches. Use the OS supplied tz handlers. If *anything* base your timestamps on GMT and then move them.