(PHP 5 >= 5.3.0)
DateInterval::format — Formats the interval
Formats the interval.
format character | Description | Example values |
---|---|---|
% | Literal % | % |
Y | Years, numeric, at least 2 digits with leading 0 | 01, 03 |
y | Years, numeric | 1, 3 |
M | Months, numeric, at least 2 digits with leading 0 | 01, 03, 12 |
m | Months, numeric | 1, 3, 12 |
D | Days, numeric, at least 2 digits with leading 0 | 01, 03, 31 |
d | Days, numeric | 1, 3, 31 |
a | Total amount of days | 4, 18, 8123 |
H | Hours, numeric, at least 2 digits with leading 0 | 01, 03, 23 |
h | Hours, numeric | 1, 3, 23 |
I | Minutes, numeric, at least 2 digits with leading 0 | 01, 03, 59 |
i | Minutes, numeric | 1, 3, 59 |
S | Seconds, numeric, at least 2 digits with leading 0 | 01, 03, 57 |
s | Seconds, numeric | 1, 3, 57 |
R | Sign "-" when negative, "+" when positive | -, + |
r | Sign "-" when negative, empty when positive | -, |
Returns the formatted interval.
Hinweis:
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".
Beispiel #1 DateInterval example
<?php
$interval = new DateInterval('P2Y4DT6H8M');
echo $interval->format('%d days');
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
4 days
Beispiel #2 DateInterval and carry over points
<?php
$interval = new DateInterval('P32D');
echo $interval->format('%d days');
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
32 days
Beispiel #3 DateInterval 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');
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
31 total days 1 month, 0 days
With php 5.3, DateTime is sweet !
Here is one quick example :
<?php
/**
* A sweet interval formatting, will use the two biggest interval parts.
* On small intervals, you get minutes and seconds.
* On big intervals, you get months and days.
* Only the two biggest parts are used.
*
* @param DateTime $start
* @param DateTime|null $end
* @return string
*/
public function formatDateDiff($start, $end=null) {
if(!($start instanceof DateTime)) {
$start = new DateTime($start);
}
if($end === null) {
$end = new DateTime();
}
if(!($end instanceof DateTime)) {
$end = new DateTime($start);
}
$interval = $end->diff($start);
$doPlural = function($nb,$str){return $nb>1?$str.'s':$str;}; // adds plurals
$format = array();
if($interval->y !== 0) {
$format[] = "%y ".$doPlural($interval->y, "year");
}
if($interval->m !== 0) {
$format[] = "%m ".$doPlural($interval->m, "month");
}
if($interval->d !== 0) {
$format[] = "%d ".$doPlural($interval->d, "day");
}
if($interval->h !== 0) {
$format[] = "%h ".$doPlural($interval->h, "hour");
}
if($interval->i !== 0) {
$format[] = "%i ".$doPlural($interval->i, "minute");
}
if($interval->s !== 0) {
if(!count($format)) {
return "less than a minute ago";
} else {
$format[] = "%s ".$doPlural($interval->s, "second");
}
}
// We use the two biggest parts
if(count($format) > 1) {
$format = array_shift($format)." and ".array_shift($format);
} else {
$format = array_pop($format);
}
// Prepend 'since ' or whatever you like
return $interval->format($format);
}
?>