Bug #64347 [Asn->Nab]: Date object

From: Date: Tue, 20 Jan 2015 21:16:01 +0000
Subject: Bug #64347 [Asn->Nab]: Date object
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190086@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64347&edit=1 ID: 64347 Updated by: derick@php.net Reported by: alessio at alias2k dot com Summary: Date object -Status: Assigned +Status: Not a bug Type: Bug Package: *General Issues Operating System: Debian 6 PHP Version: Irrelevant Assigned To: derick Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is expected behaviour. +1 month and -1 month add or remove "1" from the month number. The dates look odd because the day count under or overflows. Previous Comments: ------------------------------------------------------------------------ [2013-03-04 11:14:54] alessio at alias2k dot com Description: ------------ --- From manual page: http://www.php.net/datetime.sub#refsect1-datetime.sub- description --- When you have a datetime object you can modify the date adding or subtracting month. If you do that, the result is always unpredictable, because the mktime, the sub, or the modify function, always subtract the amount of days of the previous month. I'll make an example: If I do <?php $date = new DateTime('2001-03-31'); $interval = new DateInterval('P1M'); $date->sub($interval); echo $date->format('Y-m-d'); ?> I obtain 2001-03-03 as result, but I've expected to have 2001-02-28, because the previous month is february, not 28 days first. We have the same problem if we add some months to our starting date: <?php $date = new DateTime('2001-03-31'); $interval = new DateInterval('P1M'); $date->add($interval); echo $date->format('Y-m-d'); ?> I obtain 2001-05-01 as result, but also in this case I've aspected a different result, i think the right value is 2001-04-30, because you have to add the days of the next month to obtain the right result, not the days of current month. . The problem is also the communication with mySql, because mySql calculate the right result, so the comparison between two date it's impossible Test script: --------------- <?php $date = new DateTime('2001-03-31'); $interval = new DateInterval('P1M'); $date->sub($interval); echo $date->format('Y-m-d'); ?> Expected result: ---------------- 2001-02-28 Actual result: -------------- 2001-03-03 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=64347&edit=1

« previous php.bugs (#190086) next »