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?
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?
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()
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');