<?php
function date_interval_iso(DateInterval $interval, string $default = 'PT0F') {
static $f = ['S0F', 'M0S', 'H0M', 'DT0H', 'M0D', 'P0Y', 'Y0M', 'P0M'];
static $r = ['S', 'M', 'H', 'DT', 'M', 'P', 'Y', 'P'];
return rtrim(str_replace($f, $r,
$interval->format('P%yY%mM%dDT%hH%iM%sS%fF')
), 'PT') ?: $default;
}
$date1 = new DateTimeImmutable();
$date2 = new DateTimeImmutable();
//test 0 duration DateInterval
echo date_interval_iso($date1->diff($date1)) . PHP_EOL;
//test microseconds
echo date_interval_iso($date2->diff($date1));
preferences:
25.66 ms | 406 KiB | 5 Q