Introduzione
Represents a date interval.
A date interval stores either a fixed amount of time (in years, months,
days, hours etc) or a relative time string in the format that
DateTimeImmutable's and
DateTime's constructors support.
More specifically, the information in an object of the
DateInterval class is an instruction to get from
one date/time to another date/time. This process is not always reversible.
A common way to create a DateInterval object
is by calculating the difference between two date/time objects through
DateTimeInterface::diff().
How the time part of an interval, that is hours, minutes, seconds and
microseconds, is applied when the interval is added to, or subtracted
from, a date/time object depends on how the interval was created. For an
interval created with DateInterval::__construct()
the time part covers elapsed time, whereas for an interval created with
DateInterval::createFromDateString(), or returned
by DateTimeInterface::diff(), it increments or
decrements the individual component values. The date part, that is years,
months and days, always increments or decrements the individual component
values. The two therefore only give a different result when a
timezone transition falls within the time part of the interval; see
Date/Time Arithmetic.
Since there is no well defined way to compare date intervals,
DateInterval instances are
incomparable.
For a fixed, comparable amount of elapsed time without calendar units,
such as a timeout, use Time\Duration instead,
which is available as of PHP 8.6.0.
Proprietà
Avviso
The available properties listed below depend on PHP version, and should
be considered as readonly.
For a DateInterval object created by
DateTimeImmutable::diff() or
DateTime::diff(), the properties normally stay
within the ranges given below, except when a time zone transition or
different UTC offsets are involved. An object created by the constructor
keeps the values as given, so new DateInterval('P13M') has
m set to 13.
DateInterval::createFromDateString() also keeps
the values as given, and may produce negative ones.
- y
-
Number of years.
- m
-
Number of months, from 0 to 11.
- d
-
Number of days, from 0 to 30.
This is not the total number of days between the two dates: use the
days property for that.
- h
-
Number of hours, from 0 to 23.
- i
-
Number of minutes, from 0 to 59.
- s
-
Number of seconds, from 0 to 59.
- f
-
Number of microseconds, as a fraction of a second, from
0 to 0.999999.
- invert
-
Is 1 if the interval
represents a negative time period and
0 otherwise.
See DateInterval::format().
- days
-
If the DateInterval object was created by
DateTimeImmutable::diff() or
DateTime::diff(), then this is the
total number of full days between the start and end dates. Otherwise,
days will be false.
- from_string
-
If the DateInterval object was created by
DateInterval::createFromDateString(), then
this property's value will be true, and the
date_string property will be populated. Otherwise,
the value will be false, and the y to
f, invert, and
days properties will be populated.
- date_string
-
The string used as argument to
DateInterval::createFromDateString().