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

search for in the

DateInterval::__construct> <DateTimeZone::listIdentifiers
[edit] Last updated: Fri, 17 May 2013

view this page in

Класс DateInterval

(PHP 5 >= 5.3.0)

Введение

Представление интервалов дат.

Интервалы дат хранятся либо определенном фиксированном времени (в годах, месяцах, днях, часах и т.д.) или относительной строке времени, формат который поддерживает DateTime конструктор.

Обзор классов

DateInterval {
/* Свойства */
public integer $y ;
public integer $m ;
public integer $d ;
public integer $h ;
public integer $i ;
public integer $s ;
public integer $invert ;
public mixed $days ;
/* Методы */
public __construct ( string $interval_spec )
public static DateInterval createFromDateString ( string $time )
public string format ( string $format )
}

Свойства

y

Количество лет.

m

Количество месяцев.

d

Количество дней.

h

Количество часов.

i

Количество минут.

s

Количество секунд.

invert

Принимает 1, если интервал представляет отрицательный период времени и 0 в противном случае. См. DateInterval::format().

days

Если объект DateInterval создан методом DateTime::diff(), то это суммрное число дней между начальной и конечной датами. В противном случае days примет значение FALSE.

Содержание



DateInterval::__construct> <DateTimeZone::listIdentifiers
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes DateInterval - [4 notes]
up
4
p dot scheit at ps-webforge dot com
2 years ago
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 ...
up
8
artur at qrupa dot com
5 months ago
When using DateInterval('P3M') on 30th of November you get March instead of Ferbuary.
up
0
sebastien dot michea at manaty dot net
2 years ago
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.
up
-7
jeff dot davies at yahoo dot com
2 years ago
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.

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