Solved

A function that returns the local date / time

Posted on 2004-10-24
192 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
Question by:wildzero
    9 Comments
     
    LVL 10

    Author Comment

    by:wildzero
    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
    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
    Calling that sets the enviroment timezone dosn't it, this would also effect other users on the server wouldn't it?
    0
     
    LVL 27

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP ma…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    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…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now