7 May 2013

Convert time and date from one time zone to another in PHP. Don't ever manage time adding or subtracting seconds, there is more to datetime than meets the eye.

Source code viewer
  1. // Using the default timezone used by all date/time functions in a script, this is not thread safe.
  2. $date = new DateTime('2013-06-07 15:37:00', new DateTimeZone('Europe/Tallinn'));
  3. date_default_timezone_set('Europe/London');
  4. echo date('Y-m-d H:i', $date->format('U'));
  5.  
  6. // OR
  7.  
  8. // Using the date class, this is thread safe.
  9. $date = new DateTime('2013-06-07 15:37:00', new DateTimeZone('Europe/Tallinn'));
  10. $date->setTimezone(new DateTimeZone('Pacific/Chatham'));
  11. echo $date->format('Y-m-d H:i');
Programming Language: PHP