note 107252 added to datetime.construct
| From: | joel dot kallman at gmail dot com | Date: | Tue, 24 Jan 2012 20:07:14 +0000 |
| Subject: | note 107252 added to datetime.construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185733@lists.php.net to get a copy of this message | ||
The DateTime class should not assume that because my input is a timestamp that my desired output
will be either within the same timezone as UTC or formatted in a way that contains timezone
representation. It incorrectly assumes (in my opinion) the UTC timezone when it should instead use
the timezone provided by the programmer to properly offset the date.
The class should assume that the programmer knows what they are doing when they pass a timezone that
is different from the one that is represented in the time parameter (if one exists). The expected
functionality would be that this class would automatically offset a date that contains timezone
representation (like a timestamp) to the provided timezone when the class is instantiated. This is
PHP's default way of handling timezones with the date() function and should be consistently
followed here.
For example, one would expect the two following ways to output a date would be the same. However,
the DateTime object completely ignores the timezone provided. My proposal for how this class should
function is that when a timezone is specified through the time parameter along with an additional
timezone, it should use that additional timezone to offset the date from its specified timezone
within the first parameter.
<?php
// Using the same timestamp, timezone, and format
$timestamp = 1306123200;
$timezone = 'America/New_York';
$format = 'Y-m-d H:i:s';
// Old Way
date_default_timezone_set($timezone);
echo date($format, $timestamp); // ACTUAL output "2011-05-23 00:00:00"
// New Way
$date = new DateTime('@' . $timestamp, new DateTimeZone($timezone));
echo $date->format($format); // ACTUAL output "2011-05-23 04:00:00", EXPECTED output
"2011-05-23 00:00:00"
?>
----
Server IP: 69.147.83.197
Probable Submitter: 108.203.169.225
----
Manual Page -- http://www.php.net/manual/en/datetime.construct.php
Edit -- https://master.php.net/note/edit/107252
Del: integrated -- https://master.php.net/note/delete/107252/integrated
Del: useless -- https://master.php.net/note/delete/107252/useless
Del: bad code -- https://master.php.net/note/delete/107252/bad+code
Del: spam -- https://master.php.net/note/delete/107252/spam
Del: non-english -- https://master.php.net/note/delete/107252/non-english
Del: in docs -- https://master.php.net/note/delete/107252/in+docs
Del: other reasons-- https://master.php.net/note/delete/107252
Reject -- https://master.php.net/note/reject/107252
Search -- https://master.php.net/manage/user-notes.php