php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74610 date function returns wrong value
Submitted: 2017-05-18 11:03 UTC Modified: 2017-05-18 12:18 UTC
From: shivamkss at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.1.5 OS: Mac OS Sierra
Private report: No CVE-ID: None
 [2017-05-18 11:03 UTC] shivamkss at gmail dot com
Description:
------------
date function is returning 2017-07 while adding 1 month to "2017-05-31 09:00:00". My time zone is Asia/Calcutta.

Test script:
---------------
$‌‌date = "2017-05-31 09:00:00";
return date("Y-m",strtotime("+1 month", strtotime($date)));

Expected result:
----------------
It should return 2017-06


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-18 11:34 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-05-18 11:34 UTC] requinix@php.net
2017-05-31 + 1 month = 2017-06-31 and that overflows to 2017-07-01.

If your application needs it to stay as the last day of the month (not overflow) then you need custom logic for that. For example, https://3v4l.org/SjYiE
 [2017-05-18 12:18 UTC] derick@php.net
You don't need custom logic for this at all. You can just do:

<?php
$a = new DateTimeImmutable("2017-05-31 09:00:00");
$b = $a->modify("first day of next month");
echo $b->format("Y-m");
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 20:01:30 2024 UTC