How to use the DateInterval()Function

How to create a DateInterval object

$interval = new DateInterval('P30D'); // 30 days

How to use interval strings

$interval_1 = new DateInterval('P1Y2M10D'); // 1 year, 2 months, 10 days
$interval_2 = new DateInterval('PT1H2M3S'); // 1 hour, 2 minutes, 3 seconds
$interval_3 = new DateInterval('P1Y2M10DT1H2M3S');

How to display a date interval

echo $interval_1->format('%m months, %d days');
echo $interval_1->format('%R %M months'); // '2 months, 10 days' // '+ 02 months'
echo $interval_1->format('%R %y %m %d %h %i %s'); // '+ 1 2 10 0 0 0'
echo $interval_1->format('%R%yy %mm %dd %H:%I:%S'); // '+1y 2m 10d 00:00:00'

Back