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

LVL 10
wildzeroAsked:
Who is Participating?
 
Diablo84Commented:
You could use strtotime to account for the time difference, eg:

echo date("d.m.Y",strtotime("+6 hours"));
0
 
wildzeroAuthor Commented:
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 :)

0
 
BenMorelCommented:
Hello,
You should use putenv() to set timezone.

For example in Europe :
<?php
putenv('TZ=CET'); // CET = Central European Time, replace with your TimeZone
echo date('d.m.Y');
?>

Here is a list of the timezones :
http://www.timeanddate.com/library/abbreviations/timezones/

See ya ;)
Ben
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
wildzeroAuthor Commented:
Calling that sets the enviroment timezone dosn't it, this would also effect other users on the server wouldn't it?
0
 
Diablo84Commented:
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
0
 
basiclifeCommented:
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
0
 
BenMorelCommented:
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
0
 
KvdnBergCommented:
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.
0
 
wildzeroAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.