Page 1 of 1
date code and daylight savings time
Posted: Tue Apr 04, 2006 11:21 am
by bruceg
Hello,
I am using the following code to display the date and time
Code: Select all
<?php
$curdate=date ("m / j / y h:i");
print "$curdate"
?>
it is currently 12:19 EST and the date this displays is 11:19 so it doesn't compensate for the recent time change with daylight savings time. Any way to adjust the code to reflect this change?
Posted: Tue Apr 04, 2006 11:33 am
by feyd
Posted: Tue Apr 04, 2006 11:46 am
by bruceg
where would I place this within my current code?
Posted: Tue Apr 04, 2006 11:52 am
by feyd
second argument to
date()
Posted: Tue Apr 04, 2006 3:01 pm
by pickle
I'm wondering why the date is wrong in the first place? Is the server in a different timezone than you are? date('I') (capital i) will tell you if you're in DST or not.
Posted: Tue Apr 04, 2006 3:34 pm
by bruceg
actually yes, I figured out that the code is correct, but the hosting company is one hour behind EST, so I guess there is no way around that huh?
Posted: Tue Apr 04, 2006 3:35 pm
by pickle
Well, you can shift it on the server side, but that'd just be a fix for people in your timezone. If you want to give the proper time on the page for everyone, you're going to need to do some client-side coding.
Posted: Tue Apr 04, 2006 3:37 pm
by Ambush Commander
Or add a user preference to adjust the time.
Posted: Tue Apr 04, 2006 3:40 pm
by bruceg
please show me how to do this with PHP if possible. Otherwise I might just take the time out and use just the month and date.
Posted: Tue Apr 04, 2006 3:45 pm
by Ambush Commander
Well, you'd still have the problem of users at 11:30 PM seeing it as tomorrow (or vice versa)
Okay. Basically, the user gets to define an offset, like +3:00, or -6:00. You simply add that many seconds (hours * 60 * 60) to the time, output using gmdate() and you're set.
This doesn't take into effect DST: the user will have to manually change it every time that happens. Theoretically, you can have the user define the region they live in, and then use server-side records to determine the offset (which will take DST and other stuff into effect). I'm not exactly sure how you would go about doing that though...
Posted: Wed Apr 05, 2006 1:07 pm
by chadillac
it suprised me to think that there is no way to get the users GMT offset from a PHP function, I started thinking that with a proper IP tracking API you could get their location, then based on that calculate their GMT offset, I started looking around, and someone has beat me to the punch... Its not exactly a tutorial and the guy even states that its not a working script, its a proof on concept, but I think if someone was to take the time and has the knowledge (cough cough feyd

) they could put something like this together... could be a handy script for the entire PHP community
anyways, here is the article the guy has written.
http://www.expertsrt.com/tutorials/Matt ... eZone.html
Posted: Wed Apr 05, 2006 1:19 pm
by feyd
Considering when I was living in California, my IP was in Chicago, I can tell you it's not reliable. Even now, my IP shows up as being in Virginia. Notice my location to the left..
It's better to just ask the user what their time offset is (and possibly allow them to choose to observe daylight savings) .. I always store time using gmmktime() then on demand offset that number to reflect the time relative to the observer.
If the choose automatic time synching, I use a Javascript toy or their option of selection if Javascript is disabled to pull and pass their computer's current time offset when they log in. The automatic adjustment doesn't affect their personal preference, but does allow the time to reflect what is set on the computer they are using. Otherwise, it will use their personal preference or the "server" local time if none is given.
Posted: Wed Apr 05, 2006 2:33 pm
by chadillac
I was thinking of the javascript comparision of their time settings as another method, but I hate relying on javascript... I guess what it really boils down to is how important is this feature.... are you just displaying the time up top to make it look flashy or is the time stamp of importance to the entire application... if your answer to that is the latter of the two, I would suggest the user offset selection, otherwise... the choice is yours
