php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73298 DateTime::add adding 1 month on 31st of August skips to October
Submitted: 2016-10-12 04:09 UTC Modified: 2016-10-12 04:29 UTC
From: enachesilviu at live dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: CentOS 6
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: enachesilviu at live dot com
New email:
PHP Version: OS:

 

 [2016-10-12 04:09 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-12 04:29 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-10-12 04:29 UTC] requinix@php.net
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>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 10:01:28 2024 UTC