La classe DateInterval

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

Introduction

Représente un intervalle de dates.

Un intervalle stocke un nombre fixe de durées (en années, mois, jours, heures, etc.) ou une chaîne relative à une durée dans un format compréhensible par le constructeur de la classe DateTime et DateTimeImmutable.

Plus précisement, l'information d'un objet de la classe DateInterval est une instruction pour aller d'une date/moment à une autre date/moment. Ce processus n'est pas toujours réversible.

Une façon typique de créer un objet DateInterval est en calculant la différence entre deux objets dates/moments à travers DateTimeInterface::diff().

Comme il n'y a pas une manière bien définie pour comparer les intervalles, les instances de DateInterval sont incomparables.

Synopsis de la classe

class DateInterval {
/* Propriétés */
public int $y;
public int $m;
public int $d;
public int $h;
public int $i;
public int $s;
public float $f;
public int $invert;
public mixed $days;
/* Méthodes */
public __construct(string $duration)
public static createFromDateString(string $datetime): DateInterval
public format(string $format): string
}

Propriétés

Avertissement

Les propriétés disponibles répertoriées ci-dessous dépendent de la version de PHP et doivent être considérées en lecture seule.

y

Année.

m

Nombre de mois.

d

Nombre de jours.

h

Nombre d'heures.

i

Nombre de minutes.

s

Nombre de secondes.

f

Nombre de microsecondes, en une fraction de seconde.

invert

Vaut 1 si l'intervalle représente une période de temps négative, 0 sinon. Voir la méthode DateInterval::format().

days

Si l'objet DateInterval a été créé avec la méthode DateTimeImmutable::diff() ou DateTime::diff(), alors ce sera le nombre total de jours entre la date de début et la date de fin. Sinon, days vaudra false.

from_string

Si l'objet DateInterval a été créé avec la méthode DateInterval::createFromDateString(), alors cette propriété vaudra true, et la propriété date_string sera renseignée. Sinon, cette propriété vaudra false, et les propriétés de y à f, invert, et days seront renseignées.

date_string

La chaîne de caractères utilisée comme argument pour DateInterval::createFromDateString().

Historique

Version Description
8.2.0 Les propriétés from_string et date_string ont été ajoutées aux instances de DateInterval qui ont été créés à l'aide de la méthode DateInterval::createFromDateString().
8.2.0 Seules les propriétés de y jusqu'à f, invert, et days seront visibles.
7.4.0 Les instances de DateInterval sont désormais incomparables ; auparavant, toutes les instances DateInterval était considérées comme égale.
7.1.0 La propriété f a été ajoutée.

Sommaire

add a note

User Contributed Notes 4 notes

up
45
cb
2 years ago
If you want to reverse a date interval use array_reverse and iterator_to_array. I've found using invert to be unreliable.

<?php
$start_date
= date_create("2021-01-01");
$end_date = date_create("2021-01-05"); // If you want to include this date, add 1 day

$interval = DateInterval::createFromDateString('1 day');
$daterange = new DatePeriod($start_date, $interval ,$end_date);

function
show_dates ($dr) {
foreach(
$dr as $date1){
echo
$date1->format('Y-m-d').'<br>';
}
}

show_dates ($daterange);

echo
'<br>';

// reverse the array

$daterange = array_reverse(iterator_to_array($daterange));

show_dates ($daterange);

?>

Gives
2021-01-01
2021-01-02
2021-01-03
2021-01-04

2021-01-04
2021-01-03
2021-01-02
2021-01-01
up
3
edgaras at levinson dot lt
6 months ago
There is a handy way to compare intervals by adding them to 0 dates and comparing dates instead

<?php

function compare(DateInterval $first, DateInterval $second): int
{
$firstDate = (new DateTime())->setTimestamp(0)->add($first);
$secondDate = (new DateTime())->setTimestamp(0)->add($second);

return
$firstDate <=> $secondDate;
}

echo
compare(new DateInterval('P2D'), new DateInterval('PT48H')) . PHP_EOL;
echo
compare(DateInterval::createFromDateString('2 days'), DateInterval::createFromDateString('48 hours')) . PHP_EOL;
echo
compare(DateInterval::createFromDateString('2 days'), DateInterval::createFromDateString('49 hours')) . PHP_EOL;
echo
compare(DateInterval::createFromDateString('2 days'), DateInterval::createFromDateString('47 hours')) . PHP_EOL;

?>

Outputs:
0
0
-1
1
up
2
julio dot necronomicon at gmail dot com
1 year ago
More simple example i use to add or subtract.

<?php
$Datetime
= new Datetime('NOW', new DateTimeZone('America/Bahia'));
$Datetime->add(DateInterval::createFromDateString('2 day'));

echo
$Datetime->format("Y-m-d H:i:s");
?>
up
0
anonymous at nospam dot com
17 days ago
Subtracting months may not work as you might expect.
Code shows how subtracting 2 months from consecutive days produces potentially unexpected results.

<?php
$origin
= date_create('2025-01-31T00:00:00');
$target = date_create('2024-11-26T00:00:00');

$interval = date_diff($origin, $target);
echo
"interval total days: ".$interval->format('%a days');

echo
"interval object years: ".$interval->format('%y years');
echo
"interval object months: ".$interval->format('%m months');
echo
"interval object days: ".$interval->format('%d days');

// create 3 dates that are consecutive, but in two months
$d1 = date_create('2025-01-31T00:00:00');
$d2 = date_create('2025-02-01T00:00:00');
$d3 = date_create('2025-02-02T00:00:00');

// add (negative) interval object to each date
$d1->add($interval);
$d2->add($interval);
$d3->add($interval);

echo
"Add (negative) interval object to each date. Non-consecutive results: ";
echo(
$d1->format("Y-m-d\TH:i:s"));
echo(
$d2->format("Y-m-d\TH:i:s"));
echo(
$d3->format("Y-m-d\TH:i:s"));

// reset dates
$d1 = date_create('2025-01-31T00:00:00');
$d2 = date_create('2025-02-01T00:00:00');
$d3 = date_create('2025-02-02T00:00:00');

// Use same dates, but subtract total number of days instead of adding interval object: ";
$d1->sub(new DateInterval('P'.$interval->format('%a').'D'));
$d2->sub(new DateInterval('P'.$interval->format('%a').'D'));
$d3->sub(new DateInterval('P'.$interval->format('%a').'D'));

echo
"Use same dates, but subtract total number of days instead of adding interval object. Results (consecutive): ";
echo(
$d1->format("Y-m-d\TH:i:s"));
echo(
$d2->format("Y-m-d\TH:i:s"));
echo(
$d3->format("Y-m-d\TH:i:s"));

$d1 = date_create('2025-01-31T00:00:00');
$d2 = date_create('2025-02-01T00:00:00');
$d3 = date_create('2025-02-02T00:00:00');

// do month separately for 2025-02-01T00:00:00
echo "Do month separately (interesting): ";

echo (
'Subtract '. $interval->format('%m').' months (might not expect this):');
echo (
'Jan 31 - 2 months (61 days = Dec, 31 days + Nov, 30 days) ==> Dec 1');
echo (
'Feb 1 - 2 months (62 days = Dec, 31 days + Jan, 31 days) ==> Dec 1');
$d1->sub(new DateInterval('P'.$interval->format('%m').'M'));
echo
$d1->format("Y-m-d\TH:i:s");
$d2->sub(new DateInterval('P'.$interval->format('%m').'M'));
echo
$d2->format("Y-m-d\TH:i:s");
$d3->sub(new DateInterval('P'.$interval->format('%m').'M'));
echo
$d3->format("Y-m-d\TH:i:s");
?>

Output:

interval total days: 66 days
interval object years: 0 years
interval object months: 2 months
interval object days: 5 days

create 3 dates that are consecutive, but in two months:
$d1 = 2025-01-31T00:00:00
$d2 = 2025-02-01T00:00:00
$d3 = 2025-02-02T00:00:00

Add (negative) interval object to each date. Non-consecutive results:
2024-11-26T00:00:00
2024-11-26T00:00:00
2024-11-27T00:00:00

Use same dates, but subtract total number of days instead of adding interval object. Results (consecutive):
2024-11-26T00:00:00
2024-11-27T00:00:00
2024-11-28T00:00:00

Do month separately (interesting):

Subtract 2 months (might not expect this):
Jan 31 - 2 months (61 days = Dec, 31 days + Nov, 30 days) ==> Dec 1
Feb 1 - 2 months (62 days = Dec, 31 days + Jan, 31 days) ==> Dec 1
2024-12-01T00:00:00
2024-12-01T00:00:00
2024-12-02T00:00:00
To Top