note 107331 added to function.strtotime
| From: | cristinawithout | Date: | Tue, 31 Jan 2012 21:15:28 +0000 |
| Subject: | note 107331 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185810@lists.php.net to get a copy of this message | ||
WARNING when using "next month", "last month", "+1 month", "-1
month" or any combination of +/-X months. It will give non-intuitive results on Jan 30th and
31st.
As described at : http://derickrethans.nl/obtaining-the-next-month-in-php.html
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'next month' );
echo $d->format( 'F' ), "\n";
?>
In the above, using "next month" on January 31 will output "March" even though
you might want it to output "February". ("+1 month" will give the same result.
"last month", "-1 month" are similarly affected, but the results would be seen
at beginning of March.)
The way to get what people would generally be looking for when they say "next month" even
on Jan 30 and Jan 31 is to use "first day of next month":
<?php
$d = new DateTime( '2010-01-08' );
$d->modify( 'first day of next month' );
echo $d->format( 'F' ), "\n";
?>
<?php
$d = new DateTime( '2010-01-08' );
$d->modify( 'first day of +1 month' );
echo $d->format( 'F' ), "\n";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 98.189.138.115
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/107331
Del: integrated -- https://master.php.net/note/delete/107331/integrated
Del: useless -- https://master.php.net/note/delete/107331/useless
Del: bad code -- https://master.php.net/note/delete/107331/bad+code
Del: spam -- https://master.php.net/note/delete/107331/spam
Del: non-english -- https://master.php.net/note/delete/107331/non-english
Del: in docs -- https://master.php.net/note/delete/107331/in+docs
Del: other reasons-- https://master.php.net/note/delete/107331
Reject -- https://master.php.net/note/reject/107331
Search -- https://master.php.net/manage/user-notes.php