Last active
August 29, 2015 14:23
-
-
Save onlab/71a863feed01f79e7b7a to your computer and use it in GitHub Desktop.
PHP DateTime bug when using DateTimeZone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# 1st case | |
$baseDate = \DateTime::createFromFormat(\DateTime::ISO8601, '2015-06-23T18:00:00-0300'); | |
$originalTimeZone = $baseDate->getTimezone(); | |
echo 'Case #1:' . PHP_EOL; | |
echo '----- Everything works fine here -----' . PHP_EOL; | |
echo "SET America/Sao_Paulo \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('America/Sao_Paulo')); | |
echo "SET Original TZ \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone($originalTimeZone); | |
echo "SET America/Sao_Paulo \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('America/Sao_Paulo')); | |
echo '----- Time will be changed -3 hours (bug) -----' . PHP_EOL; | |
echo "SET Original TZ \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone($originalTimeZone); | |
echo PHP_EOL; | |
# 2st case | |
$baseDate = \DateTime::createFromFormat(\DateTime::ISO8601, '2015-06-23T18:00:00-0300'); | |
echo 'Case #2:' . PHP_EOL; | |
echo '----- Everything works fine here -----' . PHP_EOL; | |
echo "SET GMT-3 \t\t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('GMT-3')); | |
echo "SET America/Sao_Paulo \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('America/Sao_Paulo')); | |
echo '----- Time will be changed -3 hours (bug) -----' . PHP_EOL; | |
echo "SET GMT-3 \t\t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('GMT-3')); | |
echo '----- Everything works fine here -----' . PHP_EOL; | |
echo "SET America/Sao_Paulo \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('America/Sao_Paulo')); | |
echo PHP_EOL; | |
# 3nd case | |
echo 'Case #3:' . PHP_EOL; | |
$baseDate = \DateTime::createFromFormat(\DateTime::ISO8601, '2015-06-23T18:00:00-0300'); | |
echo '----- Everything works fine here -----' . PHP_EOL; | |
echo "SET America/Sao_Paulo \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('America/Sao_Paulo')); | |
echo "SET GMT-3 \t\t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('GMT-3')); | |
echo "SET America/Sao_Paulo \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('America/Sao_Paulo')); | |
echo '----- Time will be changed -3 hours (bug) -----' . PHP_EOL; | |
echo "SET America/Sao_Paulo \t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('America/Sao_Paulo')); | |
echo '----- Everything works fine here -----' . PHP_EOL; | |
echo "SET GMT-3 \t\t" . $baseDate->format(\DateTime::ISO8601) . PHP_EOL; | |
$baseDate->setTimezone(new \DateTimeZone('GMT-3')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment