PHP Velho Oeste 2024

DateInterval::format

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DateInterval::formatФорматирует интервал

Описание

public DateInterval::format(string $format): string

Форматирует интервал.

Список параметров

format

В строке format распознаются следующие символы. Каждому такому символу должен предшествовать знак процента (%).
Символ в строкеformat Описание Пример значения
% Символ % %
Y Годы, число, минимум две цифры с ведущими нулями 01, 03
y Годы, число 1, 3
M Месяцы, число, минимум две цифры с ведущими нулями 01, 03, 12
m Месяцы, число 1, 3, 12
D Дни, число, минимум две цифры с ведущими нулями 01, 03, 31
d Дни, число 1, 3, 31
a Общее количество дней в качестве результата выполнения DateTime::diff(), либо unknown 4, 18, 8123
H Часы, число, минимум две цифры с ведущими нулями 01, 03, 23
h Часы, число 1, 3, 23
I Минуты, число, минимум две цифры с ведущими нулями 01, 03, 59
i Минуты, число 1, 3, 59
S Секунды, число, минимум две цифры с ведущими нулями 01, 03, 57
s Секунды, число 1, 3, 57
F Микросекунды, число, как минимум 6 цифр с ведущим 0 007701, 052738, 428291
f Микросекунды, число 7701, 52738, 428291
R Знак "-" при отрицательном числе, "+" при положительном -, +
r Знак "-" при отрицательном числе, пусто при положительном -,

Возвращаемые значения

Возвращает отформатированный интервал.

Список изменений

Версия Описание
7.2.12 Формат F и f теперь всегда будет положительным.
7.1.0 Добавлены форматирующие символы F и f.

Примеры

Пример #1 Пример использования DateInterval

<?php

$interval
= new DateInterval('P2Y4DT6H8M');
echo
$interval->format('%d days');

?>

Результат выполнения приведённого примера:

4 days

Пример #2 DateInterval и перенос единиц

<?php

$interval
= new DateInterval('P32D');
echo
$interval->format('%d days');

?>

Результат выполнения приведённого примера:

32 days

Пример #3 DateInterval и DateTime::diff() с модификаторами %a и %d

<?php

$january
= new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);

// %a выведет общее количество дней.
echo $interval->format('%a total days')."\n";

// В то время как %d выведет только число дней, не покрытых месяцем
echo $interval->format('%m month, %d days');

?>

Результат выполнения приведённого примера:

31 total days
1 month, 0 days

Примечания

Замечание:

Метод DateInterval::format() не делает пересчёта и переноса единиц времени при переполнении значений дат. Это ожидаемое поведение, поскольку невозможно реализовать перенос при значениях, например, "32 дня", которые могут интерпретироваться по-разному, начиная "1 месяц и 4 дня" и заканчивая "1 месяц и 1 день".

Смотрите также

  • DateTime::diff() - Возвращает разницу между двумя объектами DateTime

add a note add a note

User Contributed Notes 12 notes

up
58
glavic at gmail dot com
10 years ago
How to easy recalculate carry over points:

<?php
class DateIntervalEnhanced extends DateInterval {

    public function
recalculate()
    {
       
$from = new DateTime;
       
$to = clone $from;
       
$to = $to->add($this);
       
$diff = $from->diff($to);
        foreach (
$diff as $k => $v) $this->$k = $v;
        return
$this;
    }

}

$di = new DateIntervalEnhanced('PT3600S');
echo
"Instead of " . $di->format('%h:%i:%s') . " it outputs " . $di->recalculate()->format('%h:%i:%s');
# output will be: "Instead of 0:0:3600 it outputs 1:0:0"
up
22
baptiste dot place at utopiaweb dot fr
14 years ago
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);
}
?>
up
3
me at unreal4u dot com
8 years ago
If you want the difference between two timestamps in HOURS, do not forget to add the number of days:

<?php
// Bad example
$lastEntryDate = new \DateTime('2015-05-23 00:10:20', new \DateTimeZone('UTC'));
$dateDifference = $lastEntryDate->diff(new \DateTime('2015-05-25 02:35:45', new \DateTimeZone('UTC')));

var_dump((int)$dateDifference->format('%H'));
// Returns 2
if ((int)$dateDifference->format('%H') > 24) {
       
// Will never enter here, (int)$dateDifference->format('%H') will contain 0-23 ONLY
}
?>

Instead, sum the days:
<?php
// Bad example
$lastEntryDate = new \DateTime('2015-05-23 00:10:20', new \DateTimeZone('UTC'));
$dateDifference = $lastEntryDate->diff(new \DateTime('2015-05-25 02:35:45', new \DateTimeZone('UTC')));

var_dump(($dateDifference->days * 24 + (int)$dateDifference->format('%H')));
// Returns 50
if (($dateDifference->days * 24 + (int)$dateDifference->format('%H')) > 24) {
       
// Will enter here now :)
}
?>

Hope this saves somebody some time and possible bugs :)
up
5
balaclark at gmail dot com
12 years ago
Be aware that your default timezone can sometimes alter the result of a diff so that the returned months/days are incorrect.

There is a bug report at: http://bugs.php.net/bug.php?id=52480
up
2
thflori at gmail dot com
6 years ago
The fact that $dateTime->diff(new DateTime()) returns a DateInterval and the interval is something where you don't get any useful number is somebit annoying.

<?php

$longTimeAgo
= new DateTime('1999-10-23 16:29:21')

// easy workaround:
var_dump((float)$longTimeAgo->format('U.u') - (float)(new DateTime())->format('U.u'));

// or in seconds if you are not that precise
var_dump($longTimeAgo->getTimestamp() - (new DateTime())-getTimestamp());

// this function can help you if you want to stick with DateInterval objects
function interval_to_float(DateInterval $interval) {
 
$seconds = 0;
 
$days = (int)$interval->days;
  if (
$days === false || $days === -99999) { //php5.5 compatibility
   
$days = floor($interval->y * 365.2525);
   
$days += $interval->m * 30; // that's totally inaccurate
   // or: $days += floor($interval->m * 30.4167);
   
$days += $interval->d;
  }
  return (
$days * 24 * 60 * 60 +
   
$interval->h * 60 * 60 +
   
$interval->i * 60 +
   
$interval->s) * ($interval->invert ? -1 : 1) +
   
$interval->f / 1000000; // you may wonder but they are negativ and not affected by invert
}
up
6
pankajs7590 at gmail dot com
6 years ago
public function getTotalInterval($interval, $type){
        switch($type){
            case 'years':
                return $interval->format('%Y');
                break;
            case 'months':
                $years = $interval->format('%Y');
                $months = 0;
                if($years){
                    $months += $years*12;
                }
                $months += $interval->format('%m');
                return $months;
                break;
            case 'days':
                return $interval->format('%a');
                break;
            case 'hours':
                $days = $interval->format('%a');
                $hours = 0;
                if($days){
                    $hours += 24 * $days;
                }
                $hours += $interval->format('%H');
                return $hours;
                break;
            case 'minutes':
                $days = $interval->format('%a');
                $minutes = 0;
                if($days){
                    $minutes += 24 * 60 * $days;
                }
                $hours = $interval->format('%H');
                if($hours){
                    $minutes += 60 * $hours;
                }
                $minutes += $interval->format('%i');
                return $minutes;
                break;
            case 'seconds':
                $days = $interval->format('%a');
                $seconds = 0;
                if($days){
                    $seconds += 24 * 60 * 60 * $days;
                }
                $hours = $interval->format('%H');
                if($hours){
                    $seconds += 60 * 60 * $hours;
                }
                $minutes = $interval->format('%i');
                if($minutes){
                    $seconds += 60 * $minutes;
                }
                $seconds += $interval->format('%s');
                return $seconds;
                break;
            case 'milliseconds':
                $days = $interval->format('%a');
                $seconds = 0;
                if($days){
                    $seconds += 24 * 60 * 60 * $days;
                }
                $hours = $interval->format('%H');
                if($hours){
                    $seconds += 60 * 60 * $hours;
                }
                $minutes = $interval->format('%i');
                if($minutes){
                    $seconds += 60 * $minutes;
                }
                $seconds += $interval->format('%s');
                $milliseconds = $seconds * 1000;
                return $milliseconds;
                break;
            default:
                return NULL;
        }
    }
up
2
kulakov74 at yandex dot ru
10 years ago
glavic, this does not eliminate the problem of "32 days", if you use your class for normalizing intervals of days, because the result will depend on the current month which is used for DateTime by default. And if that does not matter (because the interval is not that long) you don't have to call diff() to get the same recalculation:
$DT=new DateTime('0000-01-01'); $DT->add($oInt); echo($DT->format('Y-m-d H:i:s'));
up
5
kuzb
13 years ago
Quick class to allow you to input a time in any unit, and have it recalculate in to different denominations (for example, seconds to hours, minutes and seconds):

<?php
   
class DateIntervalEnhanced extends DateInterval
   
{

     
/* Keep in mind that a year is seen in this class as 365 days, and a month is seen as 30 days.        
         It is not possible to calculate how many days are in a given year or month without a point of 
         reference in time.*/
     
public function to_seconds()
      {
        return (
$this->y * 365 * 24 * 60 * 60) +
               (
$this->m * 30 * 24 * 60 * 60) +
               (
$this->d * 24 * 60 * 60) +
               (
$this->h * 60 * 60) +
               (
$this->i * 60) +
              
$this->s;
      }
     
      public function
recalculate()
      {
       
$seconds = $this->to_seconds();
       
$this->y = floor($seconds/60/60/24/365);
       
$seconds -= $this->y * 31536000;
       
$this->m = floor($seconds/60/60/24/30);
       
$seconds -= $this->m * 2592000;
       
$this->d = floor($seconds/60/60/24);
       
$seconds -= $this->d * 86400;
       
$this->h = floor($seconds/60/60);
       
$seconds -= $this->h * 3600;
       
$this->i = floor($seconds/60);
       
$seconds -= $this->i * 60;
       
$this->s = $seconds;
      }
    }

   
// Example usage
   
$di = new DateIntervalEnhanced('PT3600S');
   
$di->recalculate();
   
// outputs 1:0:0 instead of 0:0:3600 now!
   
echo $di->format('%H:%i:%s');
?>
up
0
nabikaz at gmail dot com
2 years ago
Important: Note that from PHP version <= 7.2.11 if you use %F or %f the result may be a negative number, which is a bug mentioned here:
https://bugs.php.net/bug.php?id=77007
And has been fixed since version 7.2.12.
up
0
pekka at gmx dot de
13 years ago
Note that `%a` is broken on Windows on VC6 builds. http://bugs.php.net/bug.php?id=51184
up
-2
zell1285 at gmail dot com
8 years ago
I was doing this: $endDate->diff($startDate)->format('%a’)
to get the number of the days between the end date and the start date. This is correct, but if I subtract 2016-04-23 00:00:00 to 2016-04-02 09:39:01 (note that in only one of the datetime values we have the time) the result is 21 (and not 20 as I expected). This is because the result of the diff is 20 days AND 14 hours, 20 mins and 59 secs. If you use the %a to get the days from a dateinterval, it will round the days (and if the hours are greater than 12, it will round days up).

So, DO NOT USE the %a formatting to get the difference in days between 2 datetime unless you are 100% sure that both the Datetime objects don't have the time.

You can solve easily with $valueDate->diff($startDate)->days that will return only the days in a dateinterval (without the fuc***g rounding).
up
-3
beowolve at gmail dot com
10 years ago
German Version of formatDateDiff:

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){
        if ($nb > 1) {
            switch ($str) {
                case 'Jahr':
                case 'Monat':
                case 'Tag':
                    return $str.'e';
                case 'Stunde':
                case 'Minute':
                case 'Sekunde':
                    return $str.'n';
            }
        } else
            return $str;
    }; // adds plurals

    $format = array();
    if($interval->y !== 0) {
        $format[] = "%y ".$doPlural($interval->y, "Jahr");
    }
    if($interval->m !== 0) {
        $format[] = "%m ".$doPlural($interval->m, "Monat");
    }
    if($interval->d !== 0) {
        $format[] = "%d ".$doPlural($interval->d, "Tag");
    }
    if($interval->h !== 0) {
        $format[] = "%h ".$doPlural($interval->h, "Stunde");
    }
    if($interval->i !== 0) {
        $format[] = "%i ".$doPlural($interval->i, "Minute");
    }
    if($interval->s !== 0) {
        $format[] = "%s ".$doPlural($interval->s, "Sekunde");
    }
   
    if(count($format)==0 || (count($format)==1 && $interval->s !== 0)) {
        return "weniger als eine Minute";
    }

    // We use the two biggest parts
    if(count($format) > 1) {
        $format = array_shift($format).", ".array_shift($format);
    } else {
        $format = array_pop($format);
    }

    // Prepend 'since ' or whatever you like
    return $interval->format($format);
}
To Top