php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73120 Wrong calculation
Submitted: 2016-09-20 08:25 UTC Modified: 2017-02-26 04:22 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: carletto90 at gmail dot com Assigned:
Status: No Feedback Package: Date/time related
PHP Version: 7.0.11 OS: Windows 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
4 + 8 = ?
Subscribe to this entry?

 
 [2016-09-20 08:25 UTC] carletto90 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/datetime.diff
---


Test script:
---------------
<?php

$date = new \DateTime('2016-03-01');
$date2 = new \DateTime('2016-03-31');
$diff = $date->diff($date2);

echo 'Years: '.$diff->y."\n";
echo 'Months: '.$diff->m."\n";
echo 'Days: '.$diff->d."\n";

?>

Expected result:
----------------
Years: 0
Months: 1
Days: 0

Actual result:
--------------
Years: 0
Months: 1
Days: 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-25 19:31 UTC] jhdxr@php.net
the current result is strange, but why the correct result is 1 month? 

2016-03-31  -  2016-03-01 ==> 30 days
2016-04-01  -  2016-03-01 ==> 1 month

I think this is the correct calculation.
 [2017-02-12 18:12 UTC] derick@php.net
-Status: Open +Status: Feedback
 [2017-02-12 18:12 UTC] derick@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


I can't reproduce this:

derick@whisky:~/dev/timelib $ php
<?php

$date = new \DateTime('2016-03-01');
$date2 = new \DateTime('2016-03-31');
$diff = $date->diff($date2);

echo 'Years: '.$diff->y."\n";
echo 'Months: '.$diff->m."\n";
echo 'Days: '.$diff->d."\n";

?>
Years: 0
Months: 0
Days: 30

Which timezone have you configured?
 [2017-02-26 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 08:01:28 2024 UTC