note 112267 added to class.dateinterval
| From: | greg at getnetez dot com | Date: | Fri, 24 May 2013 22:27:36 +0000 |
| Subject: | note 112267 added to class.dateinterval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194947@lists.php.net to get a copy of this message | ||
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');
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 184.183.23.101
----
Manual Page -- http://php.net/manual/en/class.dateinterval.php
Edit -- https://master.php.net/note/edit/112267
Del: integrated -- https://master.php.net/note/delete/112267/integrated
Del: useless -- https://master.php.net/note/delete/112267/useless
Del: bad code -- https://master.php.net/note/delete/112267/bad+code
Del: spam -- https://master.php.net/note/delete/112267/spam
Del: non-english -- https://master.php.net/note/delete/112267/non-english
Del: in docs -- https://master.php.net/note/delete/112267/in+docs
Del: other reasons-- https://master.php.net/note/delete/112267
Reject -- https://master.php.net/note/reject/112267
Search -- https://master.php.net/manage/user-notes.php