When using DateInterval('P3M') on 30th of November you get March instead of Ferbuary.
DateInterval sınıfı
(PHP 5 >= 5.3.0)
Giriş
Tarih aralığının gösterimi. Ya sabit bir süre (yıl, ay, gün saat sayısı gibi) ya da DateTime kurucusunun desteklediği biçemde göreli bir zaman dizgesi saklanır.
Sınıf Sözdizimi
DateInterval
{
/* Yöntemler */
}İçindekiler
- DateInterval::__construct — Yeni bir DateInterval nesnesi oluşturur
- DateInterval::createFromDateString — Dizgenin göreli kısımlarından bir DateInterval nesnesi döndürür
- DateInterval::format — Zaman aralığını biçimler
artur at qrupa dot com ¶
5 months ago
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 ...
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.
greg at getnetez dot com ¶
1 day ago
I found the comment from p dot scheit to be very helpful, but I had to reverse d2 and d1 in the ->diff() call in order to get an incremental addition to a start time. Also NOTE the occurance of PHP passing objects by reference necessitates the CLONE of the DateTime object I pass to the function otherwise my object gets modified by the function call ::
<?php
$date_from = new \DateTime('2012-05-05 14:00:00');
$segments = 36;
for($x=0;$x<=$segments;$x++){
unset($myDate);
$myDate = clone $date_from;
echo 'segment '.$x.' '.$this->getInterval($myDate,$x*$myResolution).'<br>';
}
//desired output
/*
segment 0 2012-05-05 14:10
segment 1 2012-05-05 14:20
segment 2 2012-05-05 14:30
segment 3 2012-05-05 14:40
segment 4 2012-05-05 14:50
segment 5 2012-05-05 15:00
segment 6 2012-05-05 15:10
...
*/
function getInterval($s,$a){
$d1 = new \DateTime();
$d2 = new \DateTime();
$d2->add(new \DateInterval('PT'.$a.'S'));
$iv = $d1->diff($d2);
return $s->add($iv)->format('Y-m-d H:i');
}
?>
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.
