note 108303 added to datetime.modify
| From: | Jennyjsimonds at atomicjetpacksdotcom | Date: | Sun, 15 Apr 2012 21:04:37 +0000 |
| Subject: | note 108303 added to datetime.modify | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187153@lists.php.net to get a copy of this message | ||
Note: This method modifies the object in-place. So if you want to calculate a new date but assign
the new value to a different object, this will NOT work:
<?php
$numMinutes = 25;
$oDateA = new DateTime('2012-01-01 12:00:00');
print "
Original:<br>
oDateA = {$oDateA->format('Y-m-d H-i-s')}<br>
";
$oDateB = $oDateA->modify ("+{$numMinutes} minutes");
print "
plus {$numMinutes} minutes:<br>
oDateA = {$oDateA->format('Y-m-d H-i-s')}<br>
oDateB = {$oDateB->format('Y-m-d H-i-s')}<br>
";
?>
...produces this:
oDateA = 2012-01-01 12-00-00
plus 25 minutes:
oDateA = 2012-01-01 12-25-00
oDateB = 2012-01-01 12-25-00
Use something like this instead:
<?php
$numMinutes = 25;
$oDateA = new DateTime('2012-01-01 12:00:00');
print "
<p>
Original:<br>
oDateA = {$oDateA->format('Y-m-d H-i-s')}<br>
";
$oDateB = clone $oDateA;
$oDateB->modify ("+{$numMinutes} minutes");
print "
plus {$numMinutes} minutes:<br>
oDateA = {$oDateA->format('Y-m-d H-i-s')}<br>
oDateB = {$oDateB->format('Y-m-d H-i-s')}<br>
";
?>
... produces this:
oDateA = 2012-01-01 12-00-00
plus 25 minutes:
oDateA = 2012-01-01 12-00-00
oDateB = 2012-01-01 12-25-00
----
Server IP: 208.69.120.55
Probable Submitter: 75.94.79.82
----
Manual Page -- http://www.php.net/manual/en/datetime.modify.php
Edit -- https://master.php.net/note/edit/108303
Del: integrated -- https://master.php.net/note/delete/108303/integrated
Del: useless -- https://master.php.net/note/delete/108303/useless
Del: bad code -- https://master.php.net/note/delete/108303/bad+code
Del: spam -- https://master.php.net/note/delete/108303/spam
Del: non-english -- https://master.php.net/note/delete/108303/non-english
Del: in docs -- https://master.php.net/note/delete/108303/in+docs
Del: other reasons-- https://master.php.net/note/delete/108303
Reject -- https://master.php.net/note/reject/108303
Search -- https://master.php.net/manage/user-notes.php