Simple library for manipulating with date intervals in PHP.
- PHP 7.0
$ composer require rm/dateinterval
namespace RM\DateInterval;
new DateInterval; // 0 seconds
new DateInterval('PT0S'); // 0 seconds
new DateInterval(new \DateInterval('PT0S')); // 0 seconds
new DateInterval('-P1D'); // - 1 day
new DateInterval('+2 minutes');
new DateInterval(10); // 10 seconds
new DateInterval(-1000); // -1000 seconds
new DateInterval(5.5); // 6 seconds
Allowing summarize multiple intervals into one.
$interval = new \RM\DateInterval;
foreach ([
'PT10S', // 0:10
'PT13M14S', // 13:24
'PT3M3S', // 16:27
'PT4M58S', // 21:25
] as $data) {
$interval->add($data);
}
echo $interval->format('%i:%s'); // '21:25'
Usable for comparing.
(new \RM\DateInterval('+1 day'))->toSeconds(); // int(86400)
Generating standardized string
, for storing interval to database.
$now = new DateTime;
$next = clone $now;
$next->modify('+1 day') // 1 day
->modify('+14 hours') // 1 day 14 hours
->modify('-3 minutes') // 1 day 13 hours 57 mins
->modify('13 seconds'); // 1 day 13 hours 57 mins 13 seconds
$interval = new \RM\DateInterval($now->diff($next));
echo (string) $interval; // P1DT13H57M13S
$interval = new \RM\DateInterval($string);
echo $interval->format('%dd %hh %im %ss'); // 1d 13h 57m 13s
Alternatively it's possible use only static method RM\DateInterval::parse(DateInterval $interval)
.