Link to home
Start Free TrialLog in
Avatar of wildzero
wildzero

asked on

A function that returns the local date / time

Hey there :)

In my scripts I have been using date('d.m.Y'); to show the days date.
However the server I am using is based in the States while I am on the other side of the world, so when it says 24/10/04 to me it's really 25/10/04

So what I am looking for is a way for the user to see what the server date and time is, and then say - I am 6 hours ahead, or 8 hours ahead

So a function like
localdatetime(6) { //6 hours ahead of the server
get server time, add 6 hours
return = local
}

Or is there is a better way?
Would like to get the date and get the time, pref seperate functions.

Cheers guys :D

Avatar of wildzero
wildzero

ASKER

The time difference (6 hrs ahead or whatever) would be hardcoded into the script, not a variable that will get modiyed, really it's just a once off but would prefer a function like above as it could be handy code in other scripts :)

SOLUTION
Avatar of BenMorel
BenMorel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Calling that sets the enviroment timezone dosn't it, this would also effect other users on the server wouldn't it?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or the more manual way:

echo date("d.m.Y H:i:s",time()+21600);

where +21600 is + the number of seconds in 6 hours
Am I correct in understanding that you want anyone viewing the page to get the time in their local time? You'll need to query the browser to find out what time zone it's in
From http://fr.php.net/manual/en/function.putenv.php :
"Adds setting to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state. "

;)
Ben
First of all to make the script workable on servers with different time zones I would use gmdate instead of date. This will give the gmt date. That you can adjust to the local date much in the same way as Diablo84 states, where you could make the function as such:

function displayLocalDate ($gmtoffset, $dst) //$gmtoffset is the gmt + or - hours, $dst should be 1 when daylight saving time is in effect for the user
{
       $localdate = gmdate("d.m.Y H:i:s", time()+(3600*($gmtoffset+$dst)));
       return $localdate;

}

DST can be checked on the server by using
date("I")
which returns 1 when dst is in effect. However if I recall correctly DST is not effective in every country and also sometimes not simultanous. So you might have to find a way to have users register when DST is in effect for them.
Thank you Diablo84, I tested your code and worked perfectly. :)
BenMorel, I read that as well, then read later on that sometimes it doesn't get 'terminated' after the request - not quite what i'm after for a multiuser enviro however I havn't tested so that article could be wrong.