|  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: enachesilviu at live dot com
New email:
PHP Version: OS:


 [2016-10-12 04:09 UTC] enachesilviu at live dot com
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:");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);

        date = calendar.getTime();

        System.out.println("\nNew value:");

Test script:

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-12 04:29 UTC]
-Status: Open +Status: Not a bug
 [2016-10-12 04:29 UTC]
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 <>.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 13 20:01:31 2024 UTC