Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


A function that returns the local date / time

Posted on 2004-10-24
Medium Priority
Last Modified: 2006-11-17
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

Question by:wildzero
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 10

Author Comment

ID: 12397015
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 :)


Assisted Solution

BenMorel earned 200 total points
ID: 12397177
You should use putenv() to set timezone.

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

Here is a list of the timezones :

See ya ;)
LVL 10

Author Comment

ID: 12398211
Calling that sets the enviroment timezone dosn't it, this would also effect other users on the server wouldn't it?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 27

Accepted Solution

Diablo84 earned 500 total points
ID: 12399037
You could use strtotime to account for the time difference, eg:

echo date("d.m.Y",strtotime("+6 hours"));
LVL 27

Expert Comment

ID: 12399061
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

Expert Comment

ID: 12406467
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

Expert Comment

ID: 12406579
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. "


Expert Comment

ID: 12408569
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
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.
LVL 10

Author Comment

ID: 12426944
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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question