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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
headzooAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.