The DateInterval class

(PHP 5 >= 5.3.0, PHP 7)

Introducere

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.

More specifically, the information in an object of the DateInterval class is an instruction to get from one date/time to another date/time. This process is not always reversible.

A common way to create a DateInterval object is by calculating the difference between two date/time objects through DateTimeInterface::diff().

Sinopsisul clasei

DateInterval {
/* Proprietăți */
public int $y ;
public int $m ;
public int $d ;
public int $h ;
public int $i ;
public int $s ;
public float $f ;
public int $invert ;
public mixed $days ;
/* Metode */
public __construct ( string $duration )
public static createFromDateString ( string $datetime ) : DateInterval|false
public format ( string $format ) : string
}

Proprietăți

y

Number of years.

m

Number of months.

d

Number of days.

h

Number of hours.

i

Number of minutes.

s

Number of seconds.

f

Number of microseconds, as a fraction of a second.

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.

Istoricul schimbărilor

Versiune Descriere
7.1.0 The f property was added.

Cuprins

add a note add a note

User Contributed Notes 3 notes

up
31
cb
2 years ago
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
up
1
julio dot necronomicon at gmail dot com
11 months ago
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");
?>
up
-2
nateb at gurutechnologies dot net
4 years ago
Many people have commented on doing a reverse interval on a date time.  I personally find a backwards year to be a little strange to think about and instead opt to work with just intervals.  This is the easiest I have found.

<?php
$one_year
= new DateInterval('P1Y');
$one_year_ago = new DateTime();
$one_year_ago->sub($one_year);
?>

Instead of:

<?php
$one_year_ago
= new DateInterval( "P1Y" );
$one_year_ago->invert = 1;
$one_year_ago = new DateTime();
$one_year_ago->add($one_year);
?>
To Top