-
Notifications
You must be signed in to change notification settings - Fork 7.8k
DateTime modify with unixtimestamp (@) must work like setTimestamp #9891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hmm, the documentation says:
This seems to be a temporary measure only: https://3v4l.org/GR7Tc @derickr, can you please clarify? |
Hi welcome @goncons when you use @ format you are using a UTC time zone, currently modify does not change the time zone. If timezone origin it's America/Lima modify add "Unix Timestamp" with @ format. DateTime::modify — Alters the timestamp The "Unix Timestamp" format sets the to UTC.
substantially DateTime::__ construct, DateTime::modify and strtotime parse a date or a format, the construct if it is already present Unix Timestamp the second argument timezone not accepted. <?php
$date = new DateTime('2022-01-01', new DateTimeZone('America/Lima'));
$date = new DateTime('@' . $date->format('U'));
echo $date->format('Y-m-d H:i:s e');
echo "\r\n";
$date->modify('2021-01-01 00:00:00 America/Lima');
echo $date->format('Y-m-d H:i:s e'); // DateTime::modify Alters the timestamp, don't change timezone from UTC to America/Lima
?> Expected result:
|
Hi @hormus my point is that the Unix Timestamp it is always in UTC, but looks like the @ format is treating the timestamp as "local" Unix timestamp, see this example:
The expected result from my point of view must be:
but the obtained result is:
Why the @ format is changing the values of the timestamp ?. |
it is not a bug modify does not change timezone, the value is UTC obviously +5 hours in object created America/Lima -05:00 <?php
$timestamp = 1667595977;
//America/Lima -05:00 convert for Unix timestamp +05:00
$date = new DateTime('now', new DateTimeZone('America/Lima'));
$date->setTimestamp($timestamp); // $timestamp (UTC) automacamally convert for timezone America/Lima ex. $timestamp - (18000)
echo $date->format('U');
$date->modify('@' . $timestamp); // Set Unix timestamp $timestamp + timezone (18000)
echo "\r\n";
echo 18000 + ($timestamp) . ' = ' . $date->format('U');
$date->modify('@' . 0); // Set Unix timestamp to 0 and timezone America/Lima -05:00
echo "\r\n";
$date->modify('+' . $timestamp . ' sec'); //Add seconds to America/Lima
echo $date->format('U');
?> Expected result:
php 5.3.6 Absolute date/time statements now take effect. Previously, only relative parts were used. |
@hormus If that is the intended behaviour then there is a "bug" in the documentation or the PHP/DateTime definition of Unix Timestamp for the @ format, bacause the Unix Timestamp is Always in UTC and the @ format is treating it like is a "local" timestamp (if you have to add or subtract the timezone offset). |
Hi @goncons can you do an example of DateTime::modify which changes the timezone of how it was created? DateTime::__construct — Returns new DateTime object The $timezone parameter and the current timezone are ignored when the $datetime parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). <?php
date_default_timezone_set('UTC');
$date1 = new DateTime('@0'); // UTC
$date2 = new DateTime('now', new DateTimeZone('America/Lima'));
$date2->modify('@0'); // php >= 5.3.6 Set Unix timestamp to 0
var_dump($date1->format('Y-m-d\\TH:i:s\\Z'), $date2->format('Y-m-d\\TH:i:s\\Z')); // Output display UTC, Attention the forced output in UTC can differ from the input, if DST or if different from civil date
?> Epoch time (also known as the Unix time) 1970-01-01 00:00:00 the maintainer of the date @derickr will decide the desired behavior. I ask for a note for good code practice it is not recommended to enter the timezone as it is ignored by DateTime::modify if you want to change the timezone use DateTime::setTimeZone or if you use @ format use DateTime::__construct |
This came up in discussion with @nicolas-grekas today. The only timezone that makes sense is UTC (unless there's some concept of 'timezoned timestamps' out there in the wild I've not come across) This is presumably why the constructor ignores timezones for these dates: $m = new DateTime('@1234567890', new DateTimeZone('Europe/Paris'));
var_dump($m->getTimeStamp()); // int(1234567890)
var_dump($m->getTimeZone()->getName()); // string(6) "+00:00" (not Europe/Paris) For me the only logical expectation would be that using $m = new DateTime('now', new DateTimeZone('Europe/Paris'));
$m->modify('@1234567890');
var_dump($m->getTimeStamp()); // int(1234564290) (not 12345677890!)
var_dump($m->getTimeZone()->getName()); // string(6) "Europe/Paris" Indeed the way the docs are structured, the formats are documented independently of the methods using them, and the relevant page states To me this is clearly just a bug and the fix is obvious... |
The bug is indeed that with The "expected output" from the original report is also wrong though, the output should be:
As the |
Description
The following code:
Resulted in this output:
But I expected this output instead:
PHP Version
PHP 8.1.12
Operating System
No response
The text was updated successfully, but these errors were encountered: