php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #54402 Format string not documented correctly for %I minutes and $S seconds
Submitted: 2011-03-27 18:25 UTC Modified: 2011-05-02 21:56 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: tom dot haws at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3SVN-2011-03-27 (snap) OS: Ubuntu
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: tom dot haws at gmail dot com
New email:
PHP Version: OS:

 

 [2011-03-27 18:25 UTC] tom dot haws at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/datetime.format#Parameters
---
Note about the time diff format string:
As far as I can tell, the datetime diff format string I ended up using is not documented anywere. The PHP manual says that the format string for the time diff is a "format accepted by date()". That would look like 'Y-m-d H:i:s', but that did not work. The other documented format I could find is the one accepted by strftime. That would look like '%Y-%M-%D %H:%i:%s', but that did not give leading zeroes for minutes and seconds.

$my_birth_tz=new DateTimeZone('America/Phoenix');
$my_birth=new DateTime('1956-09-19 08:38:00',$my_birth_tz);
$my_marriage_tz=new DateTimeZone('America/Los_Angeles');
$my_marriage=new DateTime('1981-12-14 11:38:00', $my_marriage_tz);
$my_marriage_age=$my_marriage->diff($my_birth);
echo $my_marriage_age->;format('%Y-%M-%D %H:%I:%S');
// Result: 25-02-25 04:00:00


Test script:
---------------
$my_birth_tz=new DateTimeZone('America/Phoenix');
$my_birth=new DateTime('1956-09-19 08:38:00',$my_birth_tz);
$my_marriage_tz=new DateTimeZone('America/Los_Angeles');
$my_marriage=new DateTime('1981-12-14 11:38:00', $my_marriage_tz);
$my_marriage_age=$my_marriage->diff($my_birth);
echo $my_marriage_age->;format('%Y-%M-%D %H:%I:%S');
// Result: 25-02-25 04:00:00
// Note the non-documented format() string.

echo $my_marriage_age->;format('%Y-%M-%D %H:%i:%s');
// Result: 25-02-25 04:0:0
// Expected 25-02-25 04:00:00 from the documented format string.


Expected result:
----------------
// Expected 25-02-25 04:00:00 from the documented format string.

I wanted leading zeros.


Actual result:
--------------
// Result: 25-02-25 04:0:0

No leading zeros.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-27 18:29 UTC] tom dot haws at gmail dot com
The case is worse than I stated.  I was not precise about the results vs. expected.  Following the documentation strictly gives the following result:

// Result: Y-m-d H:i:s
// Expected: 25-02-25 04:00:00 from the documented format string.
 [2011-04-26 22:12 UTC] salathe@php.net
-Status: Open +Status: Bogus
 [2011-04-26 22:12 UTC] salathe@php.net
The DateTime::diff() method returns a DateInterval object, whose 
format() method you then call. The format characters are listed 
on the page for DateInterval::format() [1] which are different 
to the format characters available for date() and DateTime::format().

[1] http://www.php.net/manual/en/dateinterval.format.php
 [2011-05-02 20:42 UTC] tom dot haws at gmail dot com
The statement at http://www.php.net/datetime.format#Parameters that the format string for the time diff is a "format accepted by date()" needs to be replaced with a link to the correct page 

http://www.php.net/manual/en/dateinterval.format.php
 [2011-05-02 21:56 UTC] salathe@php.net
No, it does not.  The description of the format parameter for DateTime::format() 
is correct. The description of the format parameter for DateInterval::format() is 
also correct.

It appears that you think you are calling DateTime::format() in your example code, 
when in fact you are calling DateInterval::format().
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 15:00:02 2026 UTC