tags:

views:

59

answers:

2

I need a method which could convert a given date from one time zone to another. Something like:

Date Format: 2010-07-13 12:34:00

$newDestinationDate = $convertTimeZoneDate($dateTime, $serverTimeZone, $userTimeZone)

Please help?

+1  A: 

If you're using PHP > 5.2.0 (which is what you should do at least):

function convertTimeZoneDate($dateTime, $serverTimeZone, $userTimeZone) 
{
    $serverTimeZone = new DateTimeZone($serverTimeZone);
    $userTimeZone   = new DateTimeZone($userTimeZone);

    $dateTime       = new DateTime($dateTime, $serverTimeZone);
    $dateTime->setTimezone($userTimeZone);

    return $dateTime->format('Y-m-d H:i:s');
}

$newDestinationDate = convertTimeZoneDate('2010-07-13 12:34:00', 'Europe/London', 'America/Los_Angeles'); 
// by the way, it's convertTimeZoneDate() and not $convertTimeZoneDate()
Stefan Gehrig
Thanks Stefan..
SachinKRaj
A: 

Thanks Stefan. I needed one more query which I myself found :).

I needed current time in different timezone, which can be done with the help of following statements:

date_default_timezone_set($newTimeZone);
$dateTime = new DateTime();
$newZoneCurrentDateTime = $dateTime->format('Y-m-d H:i:s');
SachinKRaj
Or simple use `$dateTime = new DateTime('now', $newTimeZone);`.This will not affect the default internal timezone used by PHP. Depending on your needs this may be the way to go though.
Stefan Gehrig