I am creating a script that allows the user to choose their own timezone... and enter the date $ time..So the user entered date/time must be converted to GMT format while storing into the database. While retrieving from the database it should be again converted into original format. Here DST concept must also be included.
So here date can be in a variable which can be a string or array(multidimensional array also) So i tried like this.....
function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
{
$current_zone = new DateTimeZone($currentTimezone);
//$gmt = new DateTimeZone('GMT');
$date = new DateTime($time, $current_zone);
//$date->setTimezone($gmt);
$date->setTimezone(new DateTimeZone($timezoneRequired));
return $date->format('Y-m-d H:i:s');
// Convert it back to Original timezone
$date->setTimezone($current_zone);
return $date->format('Y-m-d H:i:s');
}
$time='2011-03-29 11:15:00.000';
echo "Current Date/Time is=".ConvertOneTimezoneToAnotherTimezone($time,'Asia/Kolkata','America/New_York');
but here i am only able to convert into different timezones,but i want a single function which converts date/time and also while retrieving gives original format...... please anybody help me......
something like this http://service-kl.com/code/tz_demo2/?cou=USA
<?php
function ConvertOneTimezoneToAnotherTimezone($originalDateTime, $originalTimeZone, $targetTimeZone) {
$format = 'Y-m-d H:i:s';
$dateTime = new DateTime($originalDateTime, new DateTimeZone($originalTimeZone));
$original = $dateTime->format($format);
$dateTime->setTimezone(new DateTimeZone($targetTimeZone));
$target = $dateTime->format($format);
return compact('original', 'target');
}
$dateTime = '2011-03-29 11:15:00.000';
$converted = ConvertOneTimezoneToAnotherTimezone($dateTime,'Asia/Kolkata','America/New_York');
echo sprintf('Original Date/Time is=%s', $converted['original']), PHP_EOL;
echo sprintf('Converted Date/Time is=%s', $converted['target']), PHP_EOL;