downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DateInterval::__construct> <DateTimeZone::listIdentifiers
[edit] Last updated: Fri, 25 May 2012

view this page in

The DateInterval class

(PHP 5 >= 5.3.0)

Introduction

Representation of 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 is inverted and 0 otherwise. See DateInterval::format().

days

Total number of days between the starting and ending dates in a DateTime::diff() calculation. days will be FALSE in other circumstances.

Table of Contents



DateInterval::__construct> <DateTimeZone::listIdentifiers
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes DateInterval
Anonymous 26-Oct-2011 10:16
It is helpful to note that the value of the property 'i' is never greater than 59. That is, it is not the TOTAL number of minutes in the interval. It is the number of minutes left after counting up all the hours.
For an interval of 182 minutes, 'h' is 3 and 'i' is 2.
p dot scheit at ps-webforge dot com 15-Mar-2011 08:07
If you want to convert a Timespan given in Seconds into an DateInterval Object you could dot the following:

<?php

$dv
= new DateInterval('PT'.$timespan.'S');
?>

but wenn you look at the object, only the $dv->s property is set.

As stated in the documentation to DateInterval::format

The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".

If you still want to calculate the seconds into hours / days / years, etc do the following:

<?php

$d1
= new DateTime();
$d2 = new DateTime();
$d2->add(new DateInterval('PT'.$timespan.'S'));
     
$iv = $d2->diff($d1);

?>

$iv is an DateInterval set with days, years, hours, seconds, etc ...
Anonymous 18-Feb-2011 02:42
You cannot use the ++ operator on the DateInterval fields; it has no effect on the field value. To increase a field value, you have to do it the long way, for example $diff->h = $diff->h + 1; instead of $diff->h++; (observed in PHP 5.3.1).
jeff dot davies at yahoo dot com 14-Sep-2010 08:15
This class became available in PHP 5.3. It is not present in 5.2 or earlier releases. I found this out the hard way when you PHP scripts stopped working when I deployed them onto a Yahoo server. Yahoo has 5.2 while my machine hosts 5.3.
sebastien dot michea at manaty dot net 29-Aug-2010 11:25
It would be nice that when converting a DateInterval to a string, the interval specification used to construct the object is returned (like "P2W").
I need this to serialize a DateInterval object in order to store it in a postgres DB.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites