?
Solved

A function that returns the local date / time

Posted on 2004-10-24
9
Medium Priority
?
266 Views
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

0
Comment
Question by:wildzero
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 10

Author Comment

by:wildzero
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 :)

0
 
LVL 7

Assisted Solution

by:BenMorel
BenMorel earned 200 total points
ID: 12397177
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
 
LVL 10

Author Comment

by:wildzero
ID: 12398211
Calling that sets the enviroment timezone dosn't it, this would also effect other users on the server wouldn't it?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 27

Accepted Solution

by:
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"));
0
 
LVL 27

Expert Comment

by:Diablo84
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
0
 
LVL 5

Expert Comment

by:basiclife
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
0
 
LVL 7

Expert Comment

by:BenMorel
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. "

;)
Ben
0
 
LVL 3

Expert Comment

by:KvdnBerg
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
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
 
LVL 10

Author Comment

by:wildzero
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

601 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