php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49778 DateInterval::format("%a") is always zero
Submitted: 2009-10-05 14:32 UTC Modified: 2010-03-07 19:00 UTC
Votes:7
Avg. Score:4.7 ± 0.5
Reproduced:7 of 7 (100.0%)
Same Version:6 (85.7%)
Same OS:2 (28.6%)
From: jenwelsh at yahoo dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.0 OS: Solaris 10
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jenwelsh at yahoo dot com
New email:
PHP Version: OS:

 

 [2009-10-05 14:32 UTC] jenwelsh at yahoo dot com
Description:
------------
DateInterval cannot output the total days. It always outputs 0.

Reproduce code:
---------------
---
From manual page: dateinterval.format#Return Values
---

$i=new DateInterval('P7D');
print_r($i);
echo $i->format("%d");
echo $i->format("%a");

Expected result:
----------------
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 7
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 0
)
7
7

Actual result:
--------------
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 7
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 0
)
7
0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-05 14:37 UTC] jenwelsh at yahoo dot com
DateInterval::format with %a format does work if the DateInterval is the result of getting the difference between two DateTime objects:

$d1=date_create('2009-11-02');
$d2=date_create('2009-11-09');
$i=$d2->diff($d1);

echo $i->format("%d");//7
echo $i->format("%a");//7
 [2009-10-05 16:29 UTC] Sjoerd@php.net
Thank you for your bug report.

Days is indeed not set when creating a DateInterval using the constructor. A complication with this is that it is impossible to determine the number of days when months or years are specified, since these vary in length. It is possible to fill in the days field in some cases and leave it 0 in others. In any case, it should be documented that the days field is not always available.
 [2009-10-05 16:31 UTC] Sjoerd@php.net
Derick, how would you solve this?
 [2009-10-05 18:49 UTC] derick@php.net
It's a bug, it should set this value really.
 [2009-11-17 19:23 UTC] awinningidea at gmail dot com
PHP 5.3 on windows, DateInterval::format('%a') gives 6015 for all dates.

Example
===============================

$d1 = new DateTime('1/1/2009');
$d2 = new DateTime('1/5/2009');
$diff = $d1->diff($d2);
var_dump($diff->format('%a'));

Output
-------------------------------
string '6015' (length=4)
 [2010-01-26 17:51 UTC] hellbringer at gmail dot com
$d1 = new DateTime('2010-01-01');
$d2 = new DateTime('2010-01-26');
$diff = $d1->diff($d2);


echo phpversion();
// 5.3.1

echo $_SERVER['SERVER_SOFTWARE'];
// Apache/2.2.11 (Win32) PHP/5.3.1

echo $diff->format('%a');
// 6015

echo $diff->days;
// 6015

print_r($diff);
// DateInterval Object
// (
//     [y] => 0
//     [m] => 0
//     [d] => 25
//     [h] => 0
//     [i] => 0
//     [s] => 0
//     [invert] => 0
//     [days] => 6015
// )
 [2010-02-22 22:40 UTC] johnphayes at gmail dot com
It probably should have a different bug for the Windows "6015" bug. Just 
noting that this is what I'm seeing as well, not the original bug 
description.
 [2010-03-02 12:13 UTC] yoarvi at gmail dot com
I've attached a patch that sets the number of days value to the correct one only when the DateInterval spans neither years nor months. The patch only addresses the original problem reported in this bug report i.e. in DateInterval's constructor.

The DateTime::diff() issue belongs in a separate bug.
 [2010-03-07 16:27 UTC] derick@php.net
-Status: Assigned +Status: Closed
 [2010-03-07 16:27 UTC] derick@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

This is fixed in SVN. Instead of returning 0 (or 7), the property dump will return bool(false) and the %a formatting specifier will return "(unknown)".
 [2010-03-07 18:23 UTC] derick@php.net
Automatic comment from SVN on behalf of derick
Revision: http://svn.php.net/viewvc/?view=revision&revision=295932
Log: - Update test cases after the fix for bug #49778.
 [2010-03-07 19:00 UTC] jani@php.net
Derick made a small typo in his commit message, so here's the diff:

Revision: http://svn.php.net/viewvc/?view=revision&revision=295928
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC