DateInterval::format
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
Formats the interval
Description
Formats the interval.
Parameters
formatformatcharacterDescription Example values %Literal %%YYears, numeric, at least 2 digits with leading 0 01,03yYears, numeric 1,3MMonths, numeric, at least 2 digits with leading 0 01,03,12mMonths, numeric 1,3,12DDays, numeric, at least 2 digits with leading 0 01,03,31dDays, numeric 1,3,31aTotal number of days as a result of a DateTime::diffor(unknown)otherwise4,18,8123HHours, numeric, at least 2 digits with leading 0 01,03,23hHours, numeric 1,3,23IMinutes, numeric, at least 2 digits with leading 0 01,03,59iMinutes, numeric 1,3,59SSeconds, numeric, at least 2 digits with leading 0 01,03,57sSeconds, numeric 1,3,57FMicroseconds, numeric, at least 6 digits with leading 0 007701,052738,428291fMicroseconds, numeric 7701,52738,428291RSign " -" when negative, "+" when positive-,+rSign " -" when negative, empty when positive-,
Return Values
Returns the formatted interval.
Changelog
| Version | Description |
|---|---|
| 7.2.12 | The F and f format will now always be positive. |
| 7.1.0 | The F and f format characters were added. |
Examples
DateInterval example
<?php
$interval = new DateInterval('P2Y4DT6H8M');
echo $interval->format('%d days');The above example will output:
4 daysDateInterval and carry over points
<?php
$interval = new DateInterval('P32D');
echo $interval->format('%d days');The above example will output:
32 daysDateInterval and DateTime::diff with the %a and %d modifiers
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');The above example will output:
31 total days
1 month, 0 daysNotes
The DateInterval::format method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".
See Also
DateTime::diff