Converting Time From One Timezone to Another

I'm getting a date/time from a web service that's CET time (Note: PHP's DateTimeZone has no problems with "CET" as a value), and I need to display the time on my site in Europe/London. One day I might even show it in America/New York. There's no telling.

So how do I take in a date in CET time, and display it in another timezone?
headzooAsked:
Who is Participating?
 
headzooConnect With a Mentor Author Commented:
I managed to answer the question myself (I think) after a little playing around.
<?php
/**
 * '2007-05-21 23:34:04' is the date/time returned by
 * the web service, but it's in the CET timezone.
 */
$myDate = new DateTime('2007-05-21 23:34:04', new DateTimeZone('CET'));
echo $myDate->format('Y-m-d H:i:s');
// Displays: 2007-05-21 23:34:04
 
/**
 * Now I want to show that time in the London
 * timezone
 */
$myDate->setTimeZone(new DateTimeZone('Europe/London'));
echo $myDate->format('Y-m-d H:i:s');
// Displays: 2007-05-21 22:34:04

Open in new window

0
 
CWS (haripriya)Commented:
use putenv:
<?php
 putenv('TZ=America/New_York');
 echo date('M j Y, H:m:s');
?>
0
 
sognoctCommented:
in php 5.0 there is a beautiful function : date_timezone_set

here is an example

function time_in_hk() {
    $time = mktime();
    $def_zone = date_timezone_get();
    date_timezone_set('Europe/Madrid');
    echo date('r', $time);
    date_timezone_set($def_zone);
}
0
 
headzooAuthor Commented:
By the way I am using PHP5. As far as soqnoct's answer goes, I don't understand how that takes into account the timezone I'm getting from the web service, which is in CET (i.e. UTC + 1).
0
 
CWS (haripriya)Commented:
refer this for your timezone
http://www.theprojects.org/dev/zone.txt
0
All Courses

From novice to tech pro — start learning today.