php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60960 Wrong number of days.
Submitted: 2012-02-02 19:48 UTC Modified: 2021-04-05 12:17 UTC
Votes:6
Avg. Score:3.8 ± 1.5
Reproduced:3 of 4 (75.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: robertosuursoo at yahoo dot com dot br Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: Ubuntu 11.04 64bits
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: robertosuursoo at yahoo dot com dot br
New email:
PHP Version: OS:

 

 [2012-02-02 19:48 UTC] robertosuursoo at yahoo dot com dot br
Description:
------------
The diff function is calculating the wrong number of days.

PHP 5.3.5-1ubuntu7.4 with Suhosin-Patch (cli) (built: Dec 13 2011 18:30:11) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

Test script:
---------------
<html><head><title>TEST</title></head><body>
<?php
$a = new DateTime('2012-10-19');
$b = new DateTime('2012-10-22');
$interval = $a->diff($b);
?>
<p>$interval->days:<?=$interval->days?></p>
<?php
$a = new DateTime('2012-10-20');
$b = new DateTime('2012-10-22');
$interval = $a->diff($b);
?>
<p>$interval->days:<?=$interval->days?></p>
<?php
$a = new DateTime('2012-10-21');
$b = new DateTime('2012-10-22');
$interval = $a->diff($b);
?>
<p>$interval->days:<?=$interval->days?></p>
</body></html>

Expected result:
----------------
$interval->days:3

$interval->days:2

$interval->days:1

Actual result:
--------------
$interval->days:3

$interval->days:2

$interval->days:0



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-02 20:15 UTC] carloschilazo at gmail dot com
I couldnt reproduce the problem, I get a result of 1 on:

$a = new DateTime('2012-10-21');
$b = new DateTime('2012-10-22');
$interval = $a->diff($b);

Tested Ubuntu 11 64 bits also
 [2012-02-02 21:42 UTC] anon at anon dot anon
Obviously some wretched daylight savings issue. Call:
date_default_timezone_set('UTC');
It's the only way to make any programming language's date handling sane.
 [2012-02-02 22:04 UTC] rasmus@php.net
Unable to reproduce here as well with PHP 5.3.10. Which timezone are you using? 
Most likely DST-related.
 [2012-02-03 00:50 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2012-02-03 00:50 UTC] aharvey@php.net
Yep, this happens with the Brazil/East time zone, so it's just crossing a DST 
boundary: if you look at $interval->h on the last interval, it's 23 hours from 
midnight on the 21st to midnight on the 22nd and therefore not a full day.
 [2012-02-03 13:43 UTC] robertosuursoo at yahoo dot com dot br
Thanks a lot!
 [2012-02-03 14:10 UTC] robertosuursoo at yahoo dot com dot br
The function call worked perfectly and solved the problem!

date_default_timezone_set('UTC');

Thank you very much!
 [2012-02-03 14:12 UTC] derick@php.net
-Status: Not a bug +Status: Assigned -Assigned To: +Assigned To: derick
 [2012-02-03 14:12 UTC] derick@php.net
This is a bug, it should show 1.
 [2017-10-24 07:58 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: derick +Assigned To:
 [2021-03-31 16:35 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-03-31 16:35 UTC] cmb@php.net
For reference: <https://3v4l.org/P5D2u>.
 [2021-04-05 12:17 UTC] derick@php.net
-Status: Verified +Status: Not a bug
 [2021-04-05 12:17 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This turned out to be not a bug at all.

2012-10-21 00:00 in the Brazil/East zone does not exist:

Brazil/East  Sun Oct 21 02:59:59 2012 UT = Sat Oct 20 23:59:59 2012 -03 isdst=0 gmtoff=-10800
Brazil/East  Sun Oct 21 03:00:00 2012 UT = Sun Oct 21 01:00:00 2012 -02 isdst=1 gmtoff=-7200

As Oct 20 23:59:59 is followed by Oct 01:00:00. The result therefore is not a full day, but instead 23 hours:

$ cat /tmp/bug60900.phpt
<?php
date_default_timezone_set("Brazil/East");

$a = new DateTime('2012-10-19');
$b = new DateTime('2012-10-22');
$interval = $a->diff($b);
echo $interval->format('%dD %hH %mM'), "\n";

$a = new DateTime('2012-10-20');
$b = new DateTime('2012-10-22');
$interval = $a->diff($b);
echo $interval->format('%dD %hH %mM'), "\n";

$a = new DateTime('2012-10-21');
$b = new DateTime('2012-10-22');
$interval = $a->diff($b);

echo $interval->format('%dD %hH %mM'), "\n";

Produces:

3D 0H 0M
2D 0H 0M
0D 23H 0M
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 09:01:28 2024 UTC