note 120933 added to datetime.setdate
| From: | kevin dot nadin at gmail dot com | Date: | Wed, 05 Apr 2017 09:16:58 +0000 |
| Subject: | note 120933 added to datetime.setdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-208972@lists.php.net to get a copy of this message | ||
Be carreful about this bug in php 5.6 and lower (fixed in php 7.0 and higher) :
<?php
$date = new DateTime("first day of last month");
echo $date->format('Y-m-d');
echo ' => ' ;
$date->setDate(2013, 2, 3);
echo $date->format('Y-m-d');
?>
Output <=5.6 : 2017-03-01 => 2013-02-01
Output >=7.0 : 2017-03-31 => 2013-02-03
Same goes for "Last day of last month", and the funny part, it will take the last day of
the new month setted by setDate
Example with a Leap Year :
<?php
$date = new DateTime("last day of last month");
echo $date->format('Y-m-d');
echo ' => ' ;
$date->setDate(2012, 2, 3);
echo $date->format('Y-m-d');
?>
Output <=5.6 : 2017-03-31 => 2012-02-29
Output >=7.0 : 2017-03-31 => 2012-02-03
----
Server IP: 194.50.97.16
Probable Submitter: 82.230.174.150
----
Manual Page -- http://php.net/manual/en/datetime.setdate.php
Edit -- https://master.php.net/note/edit/120933
Del: integrated -- https://master.php.net/note/delete/120933/integrated
Del: useless -- https://master.php.net/note/delete/120933/useless
Del: bad code -- https://master.php.net/note/delete/120933/bad+code
Del: spam -- https://master.php.net/note/delete/120933/spam
Del: non-english -- https://master.php.net/note/delete/120933/non-english
Del: in docs -- https://master.php.net/note/delete/120933/in+docs
Del: other reasons-- https://master.php.net/note/delete/120933
Reject -- https://master.php.net/note/reject/120933
Search -- https://master.php.net/manage/user-notes.php