DateInterval::format
PHP
Date
DateInterval::format ( string $format )
Each char must be prefixed by a percent sign (%)
Char Description Example
Y Years , 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
What is the syntax for creating a DateInterval object in PHP?
How can you represent a DateInterval of 5 days, 2 hours, and 30 minutes in PHP?
What are the available format specifiers for the DateInterval constructor?
How can you add a DateInterval to a specific DateTime object in PHP?
How do you format a DateInterval object to display only the days, hours, and minutes?
How can you calculate the total number of hours in a DateInterval object in PHP?
What is the difference between the % sign and the P sign in a DateInterval format specifier?
Can you specify negative values in a DateInterval object? If yes, how?
How can you convert a DateInterval object into a string representation in PHP?
Are there any limitations or restrictions on the values that can be used in a DateInterval object?
← PHP Date Functions
Date Difference →
Timestamp in PHP →
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com