DateInterval::format

DateInterval::format ( string $format )
Each char must be prefixed by a percent sign (%)
CharDescriptionExample
YYears , 2 digits Minimum 01, 03
y Years 2, 13
M Months, 2 digits minimum 01, 04, 11
m Months, numeric 1, 5, 12
D Days, 2 digits minimum 01, 05, 31
d Days 1, 7, 27
a Total number of days as output of DateTime::diff() or (unknown) otherwise 10, 17, 6143
H Hours, 2 digits minimum 02, 05, 23
h Hours, 1, 5, 22
I Minutes 2 digits Minimum 01, 05, 48
i Minutes 1, 7, 59
S Seconds, 2 digits minimum 01, 08, 56
s Seconds 1, 6, 47
F Microseconds, 6 digits minimum with leading 0 006607, 043878, 579321
f Microseconds 6507, 637738, 578232
R Sign "-" when negative, "+" when positive -, +
r Sign "-" when negative, empty when positive -,
How a date interval is represented

Example 1

$interval=new DateInterval('P13M36D');

echo $interval->format('%D Days')."<br>";
echo $interval->format('%m Months')."<br>";
Output is here
36 Days
13 Months

Example 2 both Days and Months

$interval=new DateInterval('P13M36D');
echo $interval->format('%D Days & %M Months');
36 Days & 13 Months

Example 3 using diff()

We can use date diff() to get the difference and then use the format to display the difference.
$date1=new DateTime('2019-05-30');
$date2=new DateTime('2013-02-01');
$difference=$date1->diff($date2);
echo $difference->format('%Y Years , %M Months, %D Days : In total %a days');
Output is here
06 Years , 03 Months, 29 Days : In total 2309 days

Example

$interval=new DateInterval('P5Y4M2DT5H3M4S');
echo "%Y Years : ".$interval->format('%Y Years')."<br>";
echo "%y Years : ".$interval->format('%y Years')."<br>";
echo "%M Months : ".$interval->format('%M Months')."<br>";
echo "%m Months : ".$interval->format('%m Months')."<br>";
echo "%D Days : ".$interval->format('%D Days')."<br>";
echo "%d Days : ".$interval->format('%d Days')."<br>";

echo "%H Hours : ".$interval->format('%H Hours')."<br>";
echo "%h Hours : ".$interval->format('%h Hours')."<br>";

echo "%I Minutes : ".$interval->format('%I Minutes')."<br>";
echo "%i MInutes : ".$interval->format('%i Minutes')."<br>";

echo "%S Seconds : ".$interval->format('%S Seconds')."<br>";
echo "%s Seconds : ".$interval->format('%s Seconds')."<br>";

//echo "%F Microseconds : ".$interval->format('%F Microseconds')."<br>";  // PHP 7
//echo "%f Microseconds : ".$interval->format('%f Microseconds')."<br>"; // PHP 7
Output is here
%Y Years : 05 Years
%y Years : 5 Years
%M Months : 04 Months
%m Months : 4 Months
%D Days : 02 Days
%d Days : 2 Days
%H Hours : 05 Hours
%h Hours : 5 Hours
%I Minutes : 03 Minutes
%i Minutes : 3 Minutes
%S Seconds : 04 Seconds
%s Seconds : 4 Seconds
%F Microseconds : 000000 Microseconds
%f Microseconds : 0 Microseconds

Questions

PHP Date Functions Date Difference Timestamp in PHP
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer