Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

DateInterval

Represent calendar instructions such as one month or a duration such as thirty days.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new DateInterval("P1M")

Represent calendar instructions such as one month or a duration such as thirty days. In an ISO interval M means months before T and minutes after T. Applying one month to a month-end can overflow into the following month. The days property is usually false for a constructed interval but contains total days for an interval from diff.

$start=new DateTimeImmutable("2024-01-31",new DateTimeZone("UTC"));
$month=new DateInterval("P1M"); $next=$start->add($month);
echo json_encode([$next->format("Y-m-d"),$month->days,$start->diff($next)->days,(new DateInterval("PT1M"))->i]);
["2024-03-02",false,31,1]

Examples

<?php
$start=new DateTimeImmutable("2024-01-31",new DateTimeZone("UTC"));
$month=new DateInterval("P1M"); $next=$start->add($month);
echo json_encode([$next->format("Y-m-d"),$month->days,$start->diff($next)->days,(new DateInterval("PT1M"))->i]);

Result

["2024-03-02",false,31,1]

Sources