Bug #73298 [Opn->Nab]: DateTime::add adding 1 month on 31st of August skips to October
| From: | requinix@php.net | Date: | Wed, 12 Oct 2016 04:30:05 +0000 |
| Subject: | Bug #73298 [Opn->Nab]: DateTime::add adding 1 month on 31st of August skips to October | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-204618@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73298&edit=1
ID: 73298
Updated by: requinix@php.net
Reported by: enachesilviu at live dot com
Summary: DateTime::add adding 1 month on 31st of August skips
to October
-Status: Open
+Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: CentOS 6
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
PHP follows standard *nix behavior of overflowing the date: 2016-08-31 + 1 month = 2016-09-31 which
overflows to 2016-10-01.
Meanwhile Java's add() specifically does not do that: see the "Field Manipulation"
section in the documentation for java.util.Calendar <https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html>.
Previous Comments:
------------------------------------------------------------------------
[2016-10-12 04:09:18] enachesilviu at live dot com
Description:
------------
DateTime::add adding 1 month on 31st of August skips to October
I did a similar snippet in Java and I get the expected result:
import java.util.*;
import java.text.*;
import java.lang.*;
class TestingDateTime
{
public static void main (String[] args) throws java.lang.Exception
{
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
Date date = ft.parse("2016-08-31");
System.out.println("Initial value:");
System.out.println(ft.format(date));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
date = calendar.getTime();
System.out.println("\nNew value:");
System.out.println(ft.format(date));
}
}
Test script:
---------------
<?php
function printline($value) {
$separator = 'cli' == php_sapi_name() ? PHP_EOL : '<br/>';
echo $value . $separator;
}
$dateTime = new DateTime('2016-08-31 00:00:00');
printline('Initial value: ' . $dateTime->format('Y-m-d H:i:s'));
$dateTime->add(new DateInterval('P1M'));
printline('New value: ' . $dateTime->format('Y-m-d H:i:s'));
Expected result:
----------------
2016-09-30 00:00:00
Actual result:
--------------
2016-10-01 00:00:00
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73298&edit=1