Manual Address: http://php.net/manual/en/dateinterval.format.php
1 $january=NewDateTime (' 2010-01-01 ');2 $february=NewDateTime (' 2010-02-01 ');3 $interval=$january->diff ($february);4 5 //%a would output the total number of days.6 Echo $interval->format ('%r%a total days '). ' <br/> ';//+31 Total Days7 8 //While%d would only output the number of already covered by the9 //month.Ten Echo $interval->format ('%m month,%d days ');//1 month, 0 days
Format formatting parameters:
The following characters is recognized in the
format
parameter string. Each format character must is prefixed by a percent sign (
%).
format character |
Description |
Example values |
% |
Literal % |
% |
Y |
Years, numeric, at least 2 digits with leading 0 |
in the |
Y |
Years, numeric |
1, 3 |
M |
Months, numeric, at least 2 digits with leading 0 |
A. |
M |
Months, numeric |
1, 3, |
D |
Days, numeric, @ least 2 digits with leading 0 |
A. |
D |
Days, numeric |
1, 3, |
A |
Total number of days as a result of a DateTime::d iff () or (unknown)otherwise |
4, 8123 |
H |
Hours, numeric, at least 2 digits with leading 0 |
A. |
H |
Hours, numeric |
1, 3, |
I |
Minutes, numeric, at least 2 digits with leading 0 |
on the other. |
I |
Minutes, numeric |
1, 3, |
S |
Seconds, numeric, at least 2 digits with leading 0 |
A. |
S |
Seconds, numeric |
1, 3, |
R |
Sign "-" when negative, "+" when positive |
-, + |
R |
Sign "-" when negative, empty when positive |
-, |
DateTime, diff in PHP