php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63863 DateTime:setDate() day not used after modify("first/last day of...")
Submitted: 2012-12-27 18:52 UTC Modified: 2017-03-29 19:27 UTC
Votes:20
Avg. Score:4.2 ± 1.0
Reproduced:18 of 18 (100.0%)
Same Version:6 (33.3%)
Same OS:5 (27.8%)
From: brian dot feaver at sellingsource dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.1.2 OS: Mac OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
34 - 2 = ?
Subscribe to this entry?

 
 [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"

Patches

test_and_fix_bug63863 (last revision 2013-03-01 19:27 UTC by jellofishi at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-04 17:03 UTC] armin at fruux dot com
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 date
 [2013-08-02 00:38 UTC] f21 dot groups at gmail dot com
I am also seeing this in PHP 5.5.1 on Ubuntu 13.04 x64.
 [2013-09-20 01:13 UTC] php at seven dot net dot nz
Confirmed still a bug in 5.5.4 on Mac OS X.
 [2013-11-27 17:29 UTC] derick@php.net
Not quite sure whether this patch is correct, but I'll check it out!
 [2014-01-14 09:45 UTC] ieskok at ramuno dot lt
Confirmed in 5.5.7 & 5.5.8 on Ubuntu 12.04 x64
 [2014-06-25 11:01 UTC] gsdev at me dot com
Hi,

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');
 [2015-10-30 04:45 UTC] whitearchey at gmail dot com
A simple workaround:
$date->modify('+0 day');
 [2016-05-03 16:21 UTC] php-bugs at allenjb dot me dot uk
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.
 [2016-05-26 03:48 UTC] php dot net at multiwebinc dot com
This seems to happen only when using certain relative formats to create the DateTime object, such as 'last day of next month' or 'first day of this month', however 'last sat of July 2008' *does not* cause the error.
 [2016-06-29 17:34 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2016-06-29 17:34 UTC] cmb@php.net
This issue has been fixed as of PHP 5.6.19 and 7.0.4, see
<https://3v4l.org/ATGLp> and <https://3v4l.org/H3jZY>.
 [2017-03-15 06:19 UTC] grandfelix at gmail dot com
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
 [2017-03-15 06:57 UTC] requinix@php.net
-Summary: DateTime:setDate() date not used after modify("last day of...") +Summary: DateTime:setDate() day not used after modify("first/last day of...") -Status: Closed +Status: Re-Opened -PHP Version: 5.3.20 +PHP Version: 7.1.2 -Assigned To: cmb +Assigned To:
 [2017-03-15 06:57 UTC] requinix@php.net
Still present in the case of *not* using modify: https://3v4l.org/EoLFW
 [2017-03-29 19:09 UTC] php-bugs at allenjb dot me dot uk
I was reviewing this bug and I believe all the reported cases are fixed as of 7.1.3 and 7.0.17
 [2017-03-29 19:27 UTC] requinix@php.net
-Status: Re-Opened +Status: Closed -Assigned To: +Assigned To: derick
 [2017-03-29 19:27 UTC] requinix@php.net
Yup, looks fixed with the changes made for bug #73858.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Apr 01 09:01:22 2020 UTC