Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP DateTime bug when using DateTimeZone
<?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
You can’t perform that action at this time.