This is a discussion on Working with Timezones and DST within the PHP Language forums, part of the PHP Programming Forums category; Hello everyone. I am creating a JavaScript project which will allow users to see what time it is in other ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello everyone.
I am creating a JavaScript project which will allow users to see what time it is in other countries. I am wondering if there's any way to have the server work this out, without having to update a database constantly with the times and dates that certain countries set their clocks back or forward. Can this be done, or do I need to connect to a time server? Ideally, I need a time server that will return a JavaScript object containing the information I need, but I don't think one exists. Please could someone suggest the steps I need to take to quickly obtain the correct time for any given country, which will take DST into account? Sadly, to my knowledge, the UK is the only country that has hard-coded rules for when the clocks are changed, where as it seems to be a political thing for all other countries. If I have to run a database with this information, then so be it, but it seems like a lot of manual work, and I'm not sure how often I'd need to update it. Many thanks in advance. Daz. |
|
|||
|
On 27 May 2007 06:24:49 -0700, Daz <cutenfuzzy@gmail.com> wrote:
>I am creating a JavaScript project which will allow users to see what >time it is in other countries. I am wondering if there's any way to >have the server work this out, without having to update a database >constantly with the times and dates that certain countries set their >clocks back or forward. > >Can this be done, or do I need to connect to a time server? Ideally, I >need a time server that will return a JavaScript object containing the >information I need, but I don't think one exists. > >Please could someone suggest the steps I need to take to quickly >obtain the correct time for any given country, which will take DST >into account? Sadly, to my knowledge, the UK is the only country that >has hard-coded rules for when the clocks are changed, where as it >seems to be a political thing for all other countries. > >If I have to run a database with this information, then so be it, but >it seems like a lot of manual work, and I'm not sure how often I'd >need to update it. Most operating systems have comprehensive timezone data available, which gets taken into account via the TZ environment variable when formatting UNIX timestamps (which are always based on UTC). PHP 5.1 introduced improved timezone handling into the core of PHP itself, see: http://uk2.php.net/manual/en/functio...mezone-set.php (which is preferred over setting environment variables, if available). <?php foreach (array('US/Eastern', 'US/Central', 'UTC', 'Europe/London', 'Europe/Paris') as $tz) { putenv("TZ=$tz"); // or use date_default_timezone_set print date("r") . " (TZ=$tz)\n"; } ?> $ php test.php Sun, 27 May 2007 09:56:21 -0400 (TZ=US/Eastern) Sun, 27 May 2007 08:56:21 -0500 (TZ=US/Central) Sun, 27 May 2007 14:56:21 +0100 (TZ=Europe/London) Sun, 27 May 2007 15:56:21 +0200 (TZ=Europe/Paris) Sun, 27 May 2007 13:56:21 +0000 (TZ=UTC) -- Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool |
|
|||
|
On May 27, 2:58 pm, Andy Hassall <a...@andyh.co.uk> wrote:
> On 27 May 2007 06:24:49 -0700, Daz <cutenfu...@gmail.com> wrote: > > > > >I am creating a JavaScript project which will allow users to see what > >time it is in other countries. I am wondering if there's any way to > >have the server work this out, without having to update a database > >constantly with the times and dates that certain countries set their > >clocks back or forward. > > >Can this be done, or do I need to connect to a time server? Ideally, I > >need a time server that will return a JavaScript object containing the > >information I need, but I don't think one exists. > > >Please could someone suggest the steps I need to take to quickly > >obtain the correct time for any given country, which will take DST > >into account? Sadly, to my knowledge, the UK is the only country that > >has hard-coded rules for when the clocks are changed, where as it > >seems to be a political thing for all other countries. > > >If I have to run a database with this information, then so be it, but > >it seems like a lot of manual work, and I'm not sure how often I'd > >need to update it. > > Most operating systems have comprehensive timezone data available, which gets > taken into account via the TZ environment variable when formatting UNIX > timestamps (which are always based on UTC). > > PHP 5.1 introduced improved timezone handling into the core of PHP itself, > see:http://uk2.php.net/manual/en/functio...-set.php(which > is preferred over setting environment variables, if available). > > <?php > foreach (array('US/Eastern', 'US/Central', 'UTC', 'Europe/London', > 'Europe/Paris') as $tz) > { > putenv("TZ=$tz"); // or use date_default_timezone_set > print date("r") . " (TZ=$tz)\n";} > > ?> > > $ php test.php > Sun, 27 May 2007 09:56:21 -0400 (TZ=US/Eastern) > Sun, 27 May 2007 08:56:21 -0500 (TZ=US/Central) > Sun, 27 May 2007 14:56:21 +0100 (TZ=Europe/London) > Sun, 27 May 2007 15:56:21 +0200 (TZ=Europe/Paris) > Sun, 27 May 2007 13:56:21 +0000 (TZ=UTC) > > -- > Andy Hassall :: a...@andyh.co.uk ::http://www.andyh.co.ukhttp://www.and....co.uk/space:: disk and FTP usage analysis tool Hi Andy. That's wonderful. Thank you very much for such a great example. Sadly, the server I am doing this project for, is running PHP 4... Am I correct in saying that DST has been accounted for in your example? It looks like it has, but I need to be certain. Thanks again. Daz. |
|
|||
|
On 27 May 2007 07:15:14 -0700, Daz <cutenfuzzy@gmail.com> wrote:
>Hi Andy. That's wonderful. Thank you very much for such a great >example. Sadly, the server I am doing this project for, is running PHP >4... Not so bad, since the TZ environment variable will still work. From another server: $ php -v PHP 4.3.10 (cli) (built: Jan 12 2005 13:10:04) Copyright (c) 1997-2004 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies $ php test.php Sun, 27 May 2007 14:34:24 +0000 (TZ=US/Eastern) Sun, 27 May 2007 14:34:24 +0000 (TZ=US/Central) Sun, 27 May 2007 14:34:24 +0000 (TZ=UTC) Sun, 27 May 2007 15:34:24 +0100 (TZ=Europe/London) Sun, 27 May 2007 16:34:24 +0200 (TZ=Europe/Paris) OK, that server won't take US/Eastern or Central - they're wrongly coming out as +0000. It apparently takes the city-based US timezones, though: Sun, 27 May 2007 10:39:44 -0400 (TZ=America/New_York) Sun, 27 May 2007 09:39:44 -0500 (TZ=America/Chicago) Sun, 27 May 2007 14:39:44 +0000 (TZ=UTC) Sun, 27 May 2007 15:39:44 +0100 (TZ=Europe/London) Sun, 27 May 2007 16:39:44 +0200 (TZ=Europe/Paris) The supported timezone names in PHP 4 rely entirely on the operating system - in this case it seems FreeBSD's timezones list differs slightly from Linux. PHP 5's improved support is useful as it has its own timezone database, so it's more portable. >Am I correct in saying that DST has been accounted for in your >example? It looks like it has, but I need to be certain. Yes, it has. See "Europe/London" being +1 as we're in British Summer Time at the moment. -- Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool |