P粉7093078652023-08-07 13:27:57
This is another neat solution that uses DateTime methods entirely, modifying the object directly without creating a clone.
$dt = new DateTime('2012-01-31'); echo $dt->format('Y-m-d'), PHP_EOL; $day = $dt->format('j'); $dt->modify('first day of +1 month'); $dt->modify('+' . (min($day, $dt->format('t')) - 1) . ' days'); echo $dt->format('Y-m-d'), PHP_EOL;
Its output is:
2012-01-31 2012-02-29
P粉0304790542023-08-07 09:48:12
The current behavior is correct. Here's what happens internally:
1 month increases the month (originally 1) by 1. This changes the date to 2010-02-31.
February in 2010 only has 28 days, so PHP will automatically correct this problem and continue to count the days from February 1st. So the final date we got was March 3rd.
How to get the results you want:
To get the results you want, you can manually check the next month and then add the number of days in the next month.
I hope you can write this code yourself. I'm just providing specific steps.
PHP 5.3 method:
To get the correct behavior, you can use the relative time statement "first day of" introduced in PHP 5.3. This statement can be used in conjunction with "next month", "fifth month", or "8 months" to go to the first day of the specified month. Compared to the "1 month" you are currently using, you can use the following code to get the first day of the next month:
<?php $d = new DateTime( '2010-01-31' ); $d->modify( 'first day of next month' ); echo $d->format( 'F' ), "\n"; ?>
This script will correctly output February. When PHP processes this first day of next month statement, the following happens:
next month increases the month (originally 1) by 1. This changes the date to 2010-02-31.
first day of sets the number of days of the date to 1, resulting in the date becoming 2010-02-01.