Howto: Time Zone Adjustment? 
Author Message
 Howto: Time Zone Adjustment?

Newbie (to PHP) question: My server is somewhere else on the planet
and I want to keep a log of visits, recorded relative to my local time (PST)
not wherever the heck my server (and PHP) is.

Any suggestions on how I do this? Some people seem to suggest the use
of setlocale; that's fine if the country only has one time zone, but Canada
has half a dozen!



Thu, 30 Jun 2005 10:32:57 GMT  
 Howto: Time Zone Adjustment?


Quote:
> Newbie (to PHP) question: My server is somewhere else on the planet
> and I want to keep a log of visits, recorded relative to my local time (PST)
> not wherever the heck my server (and PHP) is.

> Any suggestions on how I do this? Some people seem to suggest the use
> of setlocale; that's fine if the country only has one time zone, but Canada
> has half a dozen!

Use mktime(), with an epoch stamp which has the apporpriate number of
seconds added to or subtracted from it:

<?
$offset = -2;
$stamp = date("Y-m-d H:i:s", time() + ($offset * 3600));
echo $stamp;
?>

Set $offset to the number of hours, positive or negative, that the
server is in relation to your local timezone. I'm in US Central time,
the example above would give me a timestamp in US Pacific time (2 hours
behind me). As it is now 5:58 AM locally, the snippet above output:

2003-01-12 03:58:29

You can modify the formatting within the date() command to suit your
needs.

HTH

--

--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!



Thu, 30 Jun 2005 20:00:56 GMT  
 Howto: Time Zone Adjustment?


Quote:
> Use mktime(), with an epoch stamp which has the apporpriate number of
> seconds added to or subtracted from it:

I sure blew that one, didn't I! mktime() is not needed, and appropriate
was spelled inappropriately... Fortunately the code snippet was correct.
Pardon me, I'm a few beers into the night. :)

--

--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!



Thu, 30 Jun 2005 20:04:33 GMT  
 Howto: Time Zone Adjustment?

Quote:

> Newbie (to PHP) question: My server is somewhere else on the planet
> and I want to keep a log of visits, recorded relative to my local time
> (PST) not wherever the heck my server (and PHP) is.

> Any suggestions on how I do this? Some people seem to suggest the use
> of setlocale; that's fine if the country only has one time zone, but
> Canada has half a dozen!

The best way to handle time is to use Universal Time, so-called "Zulu time"
or GMT or UTC, which is the time at Greenwich, England.  Your timezone can
be expressed as an offset from this.

What you do not necessarily know is, "where in the world is Carmen
SandeVisitor?"  But if you know a country and city, you can look up the
appropriate UTC offset.

----------------------------------
Fast automatic Paradox/Delphi table repair at a click of a mouse!
http://www.sundialservices.com/products/chimneysweep



Thu, 30 Jun 2005 22:58:33 GMT  
 Howto: Time Zone Adjustment?
On Sun, 12 Jan 2003 02:32:57 GMT, "Frank Smith"

Quote:

>Newbie (to PHP) question: My server is somewhere else on the planet
>and I want to keep a log of visits, recorded relative to my local time (PST)
>not wherever the heck my server (and PHP) is.

>Any suggestions on how I do this? Some people seem to suggest the use
>of setlocale; that's fine if the country only has one time zone, but Canada
>has half a dozen!

I use this code on a number of websites (http://www.coffeegeek.com) to
display dates in the visitors timezone.  First put this Javascript on
your homepage (or all pages):

--/ snip /--

<script language="Javascript">
<!--
var now = new Date();
var expiry = new Date(now.getTime() + 31 * 24 * 60 * 60 * 1000); //
plus 31 days
if (now.getTimezoneOffset) document.cookie = "TZ=" +
escape(now.getTimezoneOffset() * 60) + "; path=/; expires=" +
expiry.toGMTString();
-->
</script>

--/ snip /--

Then include the following functions and constants and use them
instead the corresponding PHP functions to display the date:

--/ snip /--

define('LOCAL_TZ', -intval(date('Z'))); // Timezone offset in seconds
define('LOCAL_DST', date('I'));         // Is DST or not

/**
* Returns a date string, but offset from the TZ cookie
*




*/
function tzdate($format, $date = null)
{
        if ($date == null) $date = time();
        if (isset($_COOKIE['TZ']) && is_numeric($_COOKIE['TZ'])) $tz =
$_COOKIE['TZ'];
        elseif (defined('TZ')) $tz = TZ;
        else $tz = null;
        if ($tz === null) return date($format, $date);
        return gmdate($format, max(0, $date-$tz));

Quote:
}

/**
* Returns a date string, but offset from the TZ global variable
*



*/
function tzgetdate($date = null)
{
        if ($date == null) $date = time();
        if (isset($_COOKIE['TZ']) && is_numeric($_COOKIE['TZ'])) $tz =
$_COOKIE['TZ'];
        elseif (defined('TZ')) $tz = TZ;
        else $tz = null;
        if ($tz === null) return getdate($date);
        return getdate($date - ($tz - LOCAL_TZ) + (3600 * LOCAL_DST));

Quote:
}

/**
* Converts a set of values into a timestamp, using TZ information
*









*/
function tzmktime($hour, $minute, $second, $month, $day, $year)
{
        $date = mktime($hour, $minute, $second, $month, $day, $year,
LOCAL_DST);
        if (isset($_COOKIE['TZ']) && is_numeric($_COOKIE['TZ'])) $tz =
$_COOKIE['TZ'];
        elseif (defined('TZ')) $tz = TZ;
        else $tz = null;
        if ($tz === null) return $date;
        return $date+($tz - LOCAL_TZ);

Quote:
}

--/ snip /--

You can also define the constant TZ to use as the default timezone.  

Enjoy,



Fri, 01 Jul 2005 07:15:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. time zones, daylight saving time, and universal time

2. Small Timing Adjustments in VHDL

3. Time zone in MVS

4. How to make VW TimeZone use the system time zone automatically

5. Getting Dates with time Zone

6. Setting time zone

7. time zone

8. Programming in other time zone

9. API for Time Zone

10. Time Zones

11. Time zone information

12. Setting Time Zone for my scripts

 

 
Powered by phpBB® Forum Software