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

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

6 Responses to "Adjusting the date with php"

1 | 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)

2 | 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!

3 | 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.

4 | 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 :)

5 | 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…

6 | 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.

Featured Sponsors


  • Curt: If anyone comes across this with similar issues I was able to sort out the pagination issues painlessly with easyCommentsPaginate from http://www.mush
  • Christopher: Yeah, it is indeed hard to do. And something remains elusive about why the pagination never worked. I tried everything I could find. Regardless, I
  • Jennifer: Hi Christopher, always hard to bug test stuff like that remotely. Sorry those didn't help. Glad you found a solution though :)

About


Advertisements