|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-12-27 18:52 UTC] brian dot feaver at sellingsource dot com
Description:
------------
When modifying a DateTime object with modify("last day of last month") syntax and
followed by a setDate(), the date portion of setDate() is ignored. It modifies the
year and the month, but continues to set the day portion to the last day of the
month.
If modify() is called with the absolute date instead of setDate(), it correctly
sets the date.
Test script:
---------------
<?php
$date = new DateTime('2012-03-30');
$date->modify("last day of last month");
var_dump($date->format('Y-m-d')); // correctly last day of Feb
$date->setDate(2012, 1, 30);
var_dump($date->format('Y-m-d')); // incorrect date
$date->modify('2012-01-30');
var_dump($date->format('Y-m-d')); // does set correct date
Expected result:
----------------
string(10) "2012-02-29"
string(10) "2012-01-30"
string(10) "2012-01-30"
Actual result:
--------------
string(10) "2012-02-29"
string(10) "2012-01-31"
string(10) "2012-01-30"
Patchestest_and_fix_bug63863 (last revision 2013-03-01 19:27 UTC by jellofishi at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 04:00:01 2025 UTC |
Besides from being able to reproduce this completely, it also happens when using setTimestamp(), as the day keeps being 'last day of month'. PHP version: 5.4.9 OS: Mac OS X Test script: ------------ <?php $date = new DateTime('2012-03-30'); $date->modify("last day of last month"); var_dump($date->format('Y-m-d')); // correctly last day of Feb $date->setTimestamp(1327881600); // 2012-01-30 var_dump($date->format('Y-m-d')); // incorrect date $date->modify('2012-01-30'); var_dump($date->format('Y-m-d')); // does set correct dateHi, This is happening on Mac OS X 10.8.5 PHP 5.5.0 (cli) (built: Jun 21 2013 12:09:38) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.5.0-dev, Copyright (c) 1998-2013 Zend Technologies with Xdebug v2.2.2, Copyright (c) 2002-2013, by Derick Rethans It looks to be working in PHP 5.2 but broke in PHP 5.3 onwards. http://3v4l.org/H3jZY <?php date_default_timezone_set('UTC'); $date = new DateTime(null, new DateTimeZone('UTC')); $date->setTime(0,0,0); $date->modify('next month'); $date->modify('last day of this month'); $date->setDate(2014, 1, 21); // This should be 21st Jan 2014 echo $date->format('Y-m-d');A simple workaround: $date->modify('+0 day');This also happens after using a "descriptive string" in the constructor: $dtEnd = new \DateTime("last day of last month"); var_dump($dtEnd); $dtEnd = $dtEnd->setDate(2016, 04, 03); var_dump($dtEnd); 3v4l.org shows this bug appears to be in all versions of PHP 5 + 7 to date.This is still not fixed as it was said. Example below still does not work as expected. $firstDayInMonth = new DateTime('first day of january'); echo $firstDayInMonth->setDate(date('Y'), 1, date('d'))->format('Y-m-d'); Outputs: 2017-01-01 PHP: 7.0.6-3