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 | -, |
$interval=new DateInterval('P13M36D');
echo $interval->format('%D Days')."<br>";
echo $interval->format('%m Months')."<br>";
Output is here
36 Days
13 Months
$interval=new DateInterval('P13M36D');
echo $interval->format('%D Days & %M Months');
36 Days & 13 Months
$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
$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
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.