note 112267 deleted from class.dateinterval by danbrown
| From: | danbrown@php.net | Date: | Wed, 05 Jun 2013 17:47:58 +0000 |
| Subject: | note 112267 deleted from class.dateinterval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195075@lists.php.net to get a copy of this message | ||
Note Submitter: greg at getnetez dot com
----
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');
}
?>