PHP DateInterval Class
Represents an interval of time between two dates.
Sinopsis / Synopsisclass DateInterval { /* Properties */ 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; public bool $from_string; public string $date_string; /* Methods */ public __construct(string $duration) public static createFromDateString(string $datetime): DateInterval|false public format(string $format): string }
Properties
https://blastcoding.com/en/php-dateinterval-class/#properties$y
– Number of years.$m
– Number of months.$d
– Number of days.$h
– Number of hours.$i
– Number of minutes.$s
– Number of seconds.$f
– Number of microseconds, where 1000000 microseconds is 1 second.
$invert
– If the interval represents a negative period of time, its value will be 1, otherwise it will be 0.
$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 will have a value of true
, and the $date_string property will be populated. Otherwise, the value will be false
and the $y, $m, $d, $h, $i, $s, $f, $invert, and $days properties will be populated.
$date_string
– The string used as argument to DateInterval::createFromDateString().
$one_year = new DateInterval('P1Y'); $one_year_ago = new DateTime(); $one_year_ago->sub($one_year); echo $one_year_ago->format("d-m-Y"); echo "\n".$one_year->y; echo "\n".$one_year->m; echo "\n".$one_year->d;
26-04-2022 years: 1 months:0 days: 0
Methods
__construct
https://blastcoding.com/en/php-dateinterval-class/#__constructCreate a new DateInterval object
Sinopsis / Synopsispublic DateInterval::__construct(string $duration)Sintaxis / Sintax
$dinterval = new DateInterval($duration);
$duration – should be a string representing a format that must begin with P(period) followed by an integer value and one of the following values from the table:
Period Designator | Description |
---|---|
Y | years |
M | months |
D | days |
W | weeks. Converted into days. Prior to PHP 8.0.0, can not be combined with D. |
H | hours |
M | minutes |
S | seconds |
Please note that the value of the $duration string must be an accepted format; it cannot be an empty string or left unspecified.
new DateInterval example$one_year = new DateInterval('P1Y'); $one_year_ago = new DateTime(); $one_year_ago->sub($one_year); echo $one_year_ago->format("d-m-Y");
- ‘P1Y1D’ ✔
- ‘P1D1M’ ✘
- ‘P1Y2M’ ✔
- ‘P1Y5M1H’ ✔
- ‘P1Y1W1D’ ✘
createFromDateString
https://blastcoding.com/en/php-dateinterval-class/#createFromDateStringSet a DateInterval from the relative parts of the string.
Returns a new DateInterval object if successful, false on failure.
Sinopsis / Synopsispublic static DateInterval::createFromDateString(string $datetime): DateInterval|falseSintaxis / Sintax
$dinterval = DateInterval::createFromDateString('2 weeks');
In the string, you can use:
- year/s
- month/s
- week/s
- day/s
- second/s
- + en caso de tener que sumar valor
- – en caso de restar otro valor
- ‘first’ | ‘second’ | ‘third’ | ‘fourth’ | ‘fifth’ | ‘sixth’ | ‘seventh’ | ‘eighth’ | ‘ninth’ | ‘tenth’ | ‘eleventh’ | ‘twelfth’ | ‘next’ | ‘last’ | ‘previous’ | ‘this’
format
https://blastcoding.com/php-dateinterval-class/#formatFormat the time interval.
Sinopsis / Synopsispublic DateInterval::format(string $format): string
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 number of days as a result of a DateTime::diff() or (unknown) otherwise |
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 |
F | Microseconds, numeric, at least 6 digits with leading 0 | 007701, 052738, 428291 |
f | Microseconds, numeric | 7701, 52738, 428291 |
R | Sign “–” when negative, “+” when positive | –, + |
r | Sign “–” when negative, empty when positive | –, |
Here is a fairly comprehensive example of how to use the characters:
Format interval$interval = new DateInterval('P1Y2M3DT4H5M6S'); echo $interval->format('La duración es de %r%y años, %m meses, %d días, %h horas, %i minutos y %s segundos.');
La duración es de 1 años, 2 meses, 3 días, 4 horas, 5 minutos y 6 segundos.