scriptygoddess

12 Mar, 2003

Adjusting the date with php

Posted by: Jennifer In: Script snippet

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

March 12th, 2003 at 10:25 am

Avatar

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

March 12th, 2003 at 1:19 pm

Avatar

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

March 12th, 2003 at 3:08 pm

Avatar

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

March 12th, 2003 at 3:47 pm

Avatar

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

March 12th, 2003 at 3:56 pm

Avatar

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

March 20th, 2003 at 2:51 am

Avatar

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.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements