note 74599 modified in function.date-create by derick
| From: | derick@php.net | Date: | Thu, 17 Jan 2008 09:06:48 +0000 |
| Subject: | note 74599 modified in function.date-create by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-135158@lists.php.net to get a copy of this message | ||
"String in a format accepted by strtotime()" is not 100% truth - you cannot pass timezone
info in the string used as DateTime constructor, while you can do it with strtotime(). It may be a
problem if you would like to create a date from GMT time and then display it in your local timezone,
for example:
<?php
$timeZone = 'Europe/Warsaw'; // +2 hours
date_default_timezone_set($timeZone);
$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = new DateTime($dateSrc);
echo 'date(): '.date('H:i:s', strtotime($dateSrc));
// correct! date(): 14:50:00
echo 'DateTime::format(): '.$dateTime->format('H:i:s');
// INCORRECT! DateTime::format(): 12:50:00
?>
[red. your claim that "is not 100% truth" is incorrect, you're seeing desired
behavior here. The timezone passed as 2nd argument is used as a default fall back, in case the
parsed string doesn't provide TZ information.]
So if you want to convert date between different timezones, you have to create two DateTimeZone
objects - one for the input and one for output, like this:
<?php
$timeZone = 'Europe/Warsaw'; // +2 hours
$dateSrc = '2007-04-19 12:50';
$dateTime = new DateTime($dateSrc, new DateTimeZone('GMT'));
$dateTime->setTimeZone(new DateTimeZone($timeZone));
echo 'DateTime::format(): '.$dateTime->format('H:i:s');
// CORRECT! DateTime::format(): 14:50:00
?>
I'm not sure if this is a bug or desired behaviour.
[red. you don't have to do create two DateTimeZone objects, this works too:
<?php
$timeZone = 'Europe/Warsaw'; // +2 hours
$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = new DateTime($dateSrc);
$dateTime->setTimeZone(new DateTimeZone($timeZone));
echo 'DateTime::format(): '.$dateTime->format('H:i:s');
// CORRECT! DateTime::format(): 14:50:00
?>
]
--was--
"String in a format accepted by strtotime()" is not 100% truth - you cannot pass timezone
info in the string used as DateTime constructor, while you can do it with strtotime(). It may be a
problem if you would like to create a date from GMT time and then display it in your local timezone,
for example:
<?php
$timeZone = 'Europe/Warsaw'; // +2 hours
date_default_timezone_set($timeZone);
$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = new DateTime($dateSrc);
echo 'date(): '.date('H:i:s', strtotime($dateSrc));
// correct! date(): 14:50:00
echo 'DateTime::format(): '.$dateTime->format('H:i:s');
// INCORRECT! DateTime::format(): 12:50:00
?>
So if you want to convert date between different timezones, you have to create two DateTimeZone
objects - one for the input and one for output, like this:
<?php
$timeZone = 'Europe/Warsaw'; // +2 hours
$dateSrc = '2007-04-19 12:50';
$dateTime = new DateTime($dateSrc, new DateTimeZone('GMT'));
$dateTime->setTimeZone(new DateTimeZone($timeZone));
echo 'DateTime::format(): '.$dateTime->format('H:i:s');
// CORRECT! DateTime::format(): 14:50:00
?>
I'm not sure if this is a bug or desired behaviour.
http://php.net/manual/en/function.date-create.php