×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: c4 xp
Added: Apr 22, 2016 3:34 PM
Modified: Apr 22, 2016 3:35 PM
Views: 8
Tags: timeutils
Convert date-time between timezones
  1. /**
  2.  * Convert date-time between timezones
  3.  * @see convert_tz("2011-12-13 15:16:17", "Europe/Berlin", "America/Los_Angeles")
  4.  * @see convert_tz("2011-12-13 15:16:17", date_default_timezone_get(), "Europe/Bucharest");
  5.  * @param type $fromTime
  6.  * @param type $fromTimezone
  7.  * @param type $toTimezone
  8.  * @return \DateTime
  9.  */
  10. function convert_tz($fromTime, $fromTimezone, $toTimezone) {
  11.     if (!$fromTimezone) {
  12.         $fromTimezone = 'Europe/London';
  13.     }
  14.     if (!$toTimezone) {
  15.         $toTimezone = 'Europe/London';
  16.     }
  17.     $to = new DateTimeZone($toTimezone);
  18.     $orgTime = new DateTime($fromTime, new DateTimeZone($fromTimezone));
  19.     $toTime = new DateTime($orgTime->format("c"));
  20.     $toTime->setTimezone($to);
  21.     return $toTime;//->format("c"/*Y-m-d\TH:i:sP*/));
  22. }