date code and daylight savings time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

date code and daylight savings time

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

time() + 60 * 60
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

where would I place this within my current code?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

second argument to date()
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Or add a user preference to adjust the time.
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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...
chadillac
Forum Newbie
Posts: 12
Joined: Tue Feb 28, 2006 3:30 pm
Location: Fort Lauderdale

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
chadillac
Forum Newbie
Posts: 12
Joined: Tue Feb 28, 2006 3:30 pm
Location: Fort Lauderdale

Post 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 ;)
Post Reply