If your company is dealing with international customers, you probably deal with lots of time conversions between different time zones. Time conversion can be tricky thanks to daylight savings time in some regions. Another factor that can cause some confusion is missing or not knowing your PHP default time zone.
PHP (or any other language) should have a base time zone in order to convert times. If you have not configured the default time zone for PHP, it will try to guess what the default time zone should be. It attempts to find the default time zone in the below order until it finds a valid time zone value.
Look at the code below and the results.
$dateObject = new DateTime('2011-07-10 01:00:00'); $utcTimeZone = new DateTimeZone('UTC'); $dateObject->setTimezone($utcTimeZone); echo $dateObject->format('Y-m-d H:i:s');
// In America/Chicago time zone 2011-07-10 06:00:00 // In America/Los_Angeles time zone 2011-07-10 08:00:00 // In America/New_York time zone 2011-07-10 05:00:00
/* Imagine your servers are spread geographically and are behind a DNS load balancer. Each server might have been configured with a system time zone appropriate for its location. If that’s the case, the last line will print different dates which you probably don’t want. */ // For the demonstration purposes, override the time zone. date_default_timezone_set('America/Chicago'); $dateTemplate = '2011-03-13 %02d:00:00'; for ($i = 1; $i 4; $i++) { $dateString = sprintf($dateTemplate, $i); // e.g. $dateString = '2011-03-13 01:00:00' $dateFunctionString = date('Y-m-d H:i:s', strtotime($dateString)); echo 'Source string: ' . $dateString . '
; echo 'date() function: ' . $dateFunctionString . '
; }
Source string: 2011-03-13 01:00:00 date() function: 2011-03-13 01:00:00 Source string: 2011-03-13 02:00:00 date() function: 2011-03-13 03:00:00 Source string: 2011-03-13 03:00:00 date() function: 2011-03-13 03:00:00
Notice the second result. This is not necessarily wrong. In fact, PHP converts and calculates time correctly. "2010-03-13 02:00:00" was actually "2010-03-13 03:00:00" if you were in US/Central on the spring forward day. However, it will be wrong if you were to insert hourly data into a database as it will create duplicate entries for 3/14 2 a.m.
// For the demonstration purposes, override the time zone. date_default_timezone_set('America/Chicago'); $dateObject = new DateTime('2011-03-13 01:00:00'); for ($i = 1; $i 4; $i++) { echo 'DateTime object: ' . $dateObject->format('Y-m-d H:i:s') . '
; $dateObject->modify('+1 hour'); }
DateTime object: 2011-03-13 01:00:00 DateTime object: 2011-03-13 03:00:00 DateTime object: 2011-03-13 04:00:00
Notice how the time jumps from 1 am to 3 am. You might want to use a time zone that is not affected by daylight savings time especially when you use DateTime::modify method.
These kinds of issues can be avoided by explicitly setting the default time zone using date_default_timezone_set() method somewhere in your configuration data. Using date_default_timezone_set() can also speed up "date" related function execution time as PHP doesn't have to search or try to guess the default time zone every time you use date*function. Finally, try to use "UTC" if possible so you can avoid issues related to the daylight savings time.