The DateInterval Class
The DateInterval
class allows you to work with time intervals in PHP.
It is used to store the difference between two dates or to specify an interval
in date manipulation methods. The interval is created in a special format.
Syntax
DateInterval::__construct(string $duration)
Interval Format
The format consists of the letter P
(period)
and elements:
P{y}Y{m}M{d}DT{h}H{m}M{s}S{w}W
In this format: Y
- years, M
- months,
D
- days, T
- time separator,
H
- hours, M
- minutes, S
- seconds,
W
- weeks.
You need to write the corresponding value before these letters.
For example, 3Y
will mean
three years, and 5Y2M
will denote
5
years and 2
months.
After creation, the DateInterval
object
will contain the following properties:
<?php
$interval->y // years
$interval->m // months
$interval->d // days
$interval->h // hours
$interval->i // minutes
$interval->s // seconds
$interval->f // microseconds
$interval->invert // 1 if the interval is negative
$interval->days // number of days (if created via diff())
?>
Let's look at the interval's operation with examples:
Example
Let's create an interval of 1
hour, 45
minutes and 30
seconds:
<?php
$interval = new DateInterval('PT1H45M30S');
var_dump($interval);
?>
Code execution result:
DateInterval Object
(
'y' => 0
'm' => 0
'd' => 0
'h' => 1
'i' => 45
's' => 30
)
Example
Let's create an interval of 3
weeks and 2
days:
<?php
$interval = new DateInterval('P3W2D');
var_dump($interval);
?>
Code execution result:
DateInterval Object
(
'y' => 0
'm' => 0
'd' => 23
'h' => 0
'i' => 0
's' => 0
)
Example
Let's create an interval of 5
months, 10
days and 6
hours:
<?php
$interval = new DateInterval('P5M10DT6H');
var_dump($interval);
?>
Code execution result:
DateInterval Object
(
'y' => 0
'm' => 5
'd' => 10
'h' => 6
'i' => 0
's' => 0
)
Example
Let's create an interval of 2
years and 15
minutes:
<?php
$interval = new DateInterval('P2YT15M');
var_dump($interval);
?>
Code execution result:
DateInterval Object
(
'y' => 2
'm' => 0
'd' => 0
'h' => 0
'i' => 15
's' => 0
)
Example
Let's create an interval of 1
day, 12
hours and 30
seconds:
<?php
$interval = new DateInterval('P1DT12H30S');
var_dump($interval);
?>
Code execution result:
DateInterval Object
(
'y' => 0
'm' => 0
'd' => 1
'h' => 12
'i' => 0
's' => 30
)
Example
Let's create a negative interval of 1
year and 3
months (interval backwards):
<?php
$interval = new DateInterval('P1Y3M');
$interval->invert = 1;
var_dump($interval);
?>
Code execution result:
DateInterval Object
(
'y' => 1
'm' => 3
'd' => 0
'h' => 0
'i' => 0
's' => 0
'invert' => 1
)
Example
Let's create a negative interval of 2
hours, 30
minutes (interval backwards):
<?php
$interval = new DateInterval('PT2H30M');
$interval->invert = 1;
var_dump($interval);
?>
Code execution result:
DateInterval Object
(
'y' => 0
'm' => 0
'd' => 0
'h' => 2
'i' => 30
's' => 0
'invert' => 1
)
Example
Let's add an interval to the current date:
<?php
$date = new DateTime('2023-01-01');
$interval = new DateInterval('P10D');
$date->add($interval);
echo $date->format('Y-m-d');
?>
Code execution result:
'2023-01-11'
Example
Let's calculate the difference between two dates:
<?php
$date1 = new DateTime('2023-01-01');
$date2 = new DateTime('2023-02-15');
$interval = $date1->diff($date2);
echo $interval->format('%m months %d days');
?>
Code execution result:
'1 months 14 days'
See Also
-
the
DateTime
class,
which represents a date and time -
the
DateTimeZone
class,
which represents a time zone