The DateInterval class

(PHP 5 >= 5.3.0)

Introduction

Represents a date interval.

A date interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTime's constructor supports.

Class synopsis

DateInterval {
/* Properties */
public integer $y ;
public integer $m ;
public integer $d ;
public integer $h ;
public integer $i ;
public integer $s ;
public integer $invert ;
public mixed $days ;
/* Methods */
public __construct ( string $interval_spec )
public static DateInterval createFromDateString ( string $time )
public string format ( string $format )
}

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.

invert

Is 1 if the interval represents a negative time period and 0 otherwise. See DateInterval::format().

days

If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.

Before PHP 5.4.20/5.5.4 instead of FALSE you will receive -99999 upon accessing the property.

Table of Contents

Коментарии

Автор:
If you want to reverse a date interval use array_reverse and iterator_to_array. I've found using invert to be unreliable.

<?php
$start_date 
date_create("2021-01-01");
$end_date   date_create("2021-01-05"); // If you want to include this date, add 1 day

$interval DateInterval::createFromDateString('1 day');
$daterange = new DatePeriod($start_date$interval ,$end_date);

function 
show_dates ($dr) {
    foreach(
$dr as $date1){
        echo 
$date1->format('Y-m-d').'<br>';
    }
}
 
show_dates ($daterange);
   
echo 
'<br>';

// reverse the array

$daterange array_reverse(iterator_to_array($daterange));

show_dates ($daterange);
           
?>

Gives 
 2021-01-01
 2021-01-02
 2021-01-03
 2021-01-04

 2021-01-04
 2021-01-03
 2021-01-02
 2021-01-01
2022-12-04 06:12:17
http://php5.kiev.ua/manual/ru/class.dateinterval.html
More simple example i use to add or subtract.

<?php
$Datetime 
= new Datetime('NOW', new DateTimeZone('America/Bahia'));
$Datetime->add(DateInterval::createFromDateString('2 day'));

echo 
$Datetime->format("Y-m-d H:i:s");
?>
2024-01-18 17:56:00
http://php5.kiev.ua/manual/ru/class.dateinterval.html
There is a handy way to compare intervals by adding them to 0 dates and comparing dates instead

<?php

function compare(DateInterval $firstDateInterval $second): int
{
   
$firstDate = (new DateTime())->setTimestamp(0)->add($first);
   
$secondDate = (new DateTime())->setTimestamp(0)->add($second);

    return 
$firstDate <=> $secondDate;
}

echo 
compare(new DateInterval('P2D'), new DateInterval('PT48H')) . PHP_EOL;
echo 
compare(DateInterval::createFromDateString('2 days'), DateInterval::createFromDateString('48 hours')) . PHP_EOL;
echo 
compare(DateInterval::createFromDateString('2 days'), DateInterval::createFromDateString('49 hours')) . PHP_EOL;
echo 
compare(DateInterval::createFromDateString('2 days'), DateInterval::createFromDateString('47 hours')) . PHP_EOL;

?>

Outputs:
0
0
-1
1
2024-08-01 17:58:35
http://php5.kiev.ua/manual/ru/class.dateinterval.html

    Поддержать сайт на родительском проекте КГБ