I have been trying to convert between timezones a given date and time.
Code speaks more than words, so here it is:
/**
* Returns the date and time in the new timezone.
*
* @param string $datetime:
* the date and time to change between timezones
* @param string $input_tz:
* the input timezone
* @param string $output_tz:
* the output timezone
* @return string The new date and time
*/
public function changeDateTime($datetime, $input_tz, $output_tz) {
if($input_tz == $output_tz) return $datetime;
/*
* We calculate the hour and minute offset from GMT
*/
date_default_timezone_set($output_tz);
$out_dst = date('I', $datetime) ? 1 : 0;
$out_hour_offset = intval(substr(date('O', $datetime), 1, 3)) + $out_dst;
$out_minute_offset = intval(substr(date('O', $datetime)), - 2);
date_default_timezone_set($input_tz);
$in_dst = date('I', $datetime) ? 1 : 0;
$in_hour_offset = intval(substr(date('O', $datetime), 1, 3)) + $in_dst;
$in_minute_offset = intval(substr(date('O', $datetime)), - 2);
/*
* We subtract hour and minute offsets to come up with total difference
*/
$hour_offset = $out_hour_offset - $in_hour_offset;
$minute_offset = $out_minute_offset - $in_minute_offset;
/*
* Now we must take care of changing the day/month/year if necessary, as
* well as the hour/minute for $datetime, and return that value.
*/
$date = new DateTime($datetime);
if($hour_offset > 0) {
$date->add(date_interval_create_from_date_string($hour_offset . ' hours'));
if($minute_offset > 0) $date->add(date_interval_create_from_date_string($minute_offset . ' minutes'));
} else if($hour_offset < 0) {
$date->sub(date_interval_create_from_date_string($hour_offset . ' hours'));
if($minute_offset > 0) $date->sub(date_interval_create_from_date_string($minute_offset . ' minutes'));
}
return $date->format('Y-m-d H:i:s');
}
However, it does not seem to work well. This is the code I am running to test whether it works or not:
$newdatetime = $gato->changeDateTime("2012-08-10 11:33:33", 'Europe/London', 'Europe/Madrid');
echo $newdatetime;
And this is my expected output: 2012-08-10 12:33:33
But this is my actual output: 2012-08-10 11:33:33, which means there is no change in time.
OK, try this instead:
Rather than messing about doing all that complicated maths, just let PHP do all the hard work for you 😉
See it working