Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging date and time between timezones
    primarykey
    data
    text
    <p>I have been trying to convert between timezones a given date and time. <br/>Code speaks more than words, so here it is:</p> <pre><code>/** * 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 &gt; 0) { $date-&gt;add(date_interval_create_from_date_string($hour_offset . ' hours')); if($minute_offset &gt; 0) $date-&gt;add(date_interval_create_from_date_string($minute_offset . ' minutes')); } else if($hour_offset &lt; 0) { $date-&gt;sub(date_interval_create_from_date_string($hour_offset . ' hours')); if($minute_offset &gt; 0) $date-&gt;sub(date_interval_create_from_date_string($minute_offset . ' minutes')); } return $date-&gt;format('Y-m-d H:i:s'); } </code></pre> <p>However, it does not seem to work well. This is the code I am running to test whether it works or not:</p> <pre><code>$newdatetime = $gato-&gt;changeDateTime("2012-08-10 11:33:33", 'Europe/London', 'Europe/Madrid'); echo $newdatetime; </code></pre> <p>And this is my expected output: 2012-08-10 12:33:33<br /> But this is my actual output: 2012-08-10 11:33:33, which means there is no change in time.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload