|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-10-08 06:21 UTC] christopher dot jf dot hopper at gmail dot com
Description:
------------
== PHP Version Information ==
# php --version
PHP 5.3.3 (cli) (built: May 7 2012 19:58:17)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
# cat /proc/version
Linux version 2.6.32-220.17.1.el6.i686 (mockbuild@c6b5.bsys.dev.centos.org) (gcc
version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) ) #1 SMP Tue May 15 22:09:39 BST
2012
== Describing The Problem ==
When using the static method DateTime::createFromFormat() or the new DateTime()
constructor to create a DateTime object instance from a Unix Timestamp, the
resulting date can be one day out, even though the timestamp is not. To
reproduce the bug, run the test script below.
Test script:
---------------
<?php
// Bad
$d=DateTime::createFromFormat("!Y-m-d H:i:s", "2012-11-30 00:00:00");
echo $d->getTimestamp() . PHP_EOL;
echo $d->format("Y-m-d") . PHP_EOL;
$e=new DateTime("@". $d->getTimestamp());
echo $e->getTimestamp() . PHP_EOL;
echo $e->format("Y-m-d") . PHP_EOL . PHP_EOL;
/*
1354194000
2012-11-30
1354194000
2012-11-29
*/
// Bad
$d=DateTime::createFromFormat("!Y-m-d H:i:s", "2012-11-30 00:00:00");
echo $d->getTimestamp() . PHP_EOL;
echo $d->format("Y-m-d") . PHP_EOL;
$e=DateTime::createFromFormat("U", $d->getTimestamp());
echo $e->getTimestamp() . PHP_EOL;
echo $e->format("Y-m-d") . PHP_EOL . PHP_EOL;
/*
1354194000
2012-11-30
1354194000
2012-11-29
*/
// Good
$d=DateTime::createFromFormat("!Y-m-d H:i:s", "2012-11-30 00:00:00");
echo $d->getTimestamp() . PHP_EOL;
echo $d->format("Y-m-d") . PHP_EOL;
$e=new DateTime();
$e->setTimestamp($d->getTimestamp());
echo $e->getTimestamp() . PHP_EOL;
echo $e->format("Y-m-d") . PHP_EOL;
/*
1354194000
2012-11-30
1354194000
2012-11-30
*/
Expected result:
----------------
1354194000
2012-11-30
1354194000
2012-11-30
1354194000
2012-11-30
1354194000
2012-11-30
1354194000
2012-11-30
1354194000
2012-11-30
Actual result:
--------------
1354194000
2012-11-30
1354194000
2012-11-29
1354194000
2012-11-30
1354194000
2012-11-29
1354194000
2012-11-30
1354194000
2012-11-30
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 21:00:01 2025 UTC |
This is not a bug but related to your timezone settings. The first DateTime-Object you create is created within your default timezone. And as that is east to UTC the 00:00 H is related to your local time. The timestamp though is UTC. The DateTime-Object you then create from the timestamp will use UTC as timezone (as you created it from a timestamp). And in UTC 00:00 your local time is some time *before* that (depending on your offset to UTC). You can see that when you add the following lines to your test-script: echo $d->format('c') . "\n"; echo $e->format('c') . "\n"; As it's not a bug, I'm going to close this issue.