PHP 获取前月 月初/月末 日期
程序员文章站
2022-05-15 09:16:39
...
$now = '2021-03-30';
echo 'now: '.$now.'<br><br>';
$firstDayOfThisMonth = date("Y-m-01", strtotime($now));
echo 'first day of this month: '.$firstDayOfThisMonth.'<br><br>';
$lastDayOfPrevMonth = date('Y-m-d', strtotime($firstDayOfThisMonth .' -1 day'));
echo 'last day of prev month: '.$lastDayOfPrevMonth.'<br><br>';
$firstDayOfPrevMonth = date('Y-m-d', strtotime($firstDayOfThisMonth .' -1 month'));
echo 'first day of prev month: '.$firstDayOfPrevMonth.'<br><br>';
echo '------------------------------------<br><br>';
$now = '2021-10-01';
echo 'now: '.$now.'<br><br>';
$firstDayOfThisMonth = date("Y-m-01", strtotime($now));
echo 'first day of this month: '.$firstDayOfThisMonth.'<br><br>';
$lastDayOfPrevMonth = date('Y-m-d', strtotime($firstDayOfThisMonth .' -1 day'));
echo 'last day of prev month: '.$lastDayOfPrevMonth.'<br><br>';
$firstDayOfPrevMonth = date('Y-m-d', strtotime($firstDayOfThisMonth .' -1 month'));
echo 'first day of prev month: '.$firstDayOfPrevMonth.'<br><br>';
echo '------------------------------------<br><br>';
$now = date("Y-m-d");
echo 'now: '.$now.'<br><br>';
$firstDayOfThisMonth = date("Y-m-01", strtotime($now));
echo 'first day of this month: '.$firstDayOfThisMonth.'<br><br>';
$lastDayOfPrevMonth = date('Y-m-d', strtotime($firstDayOfThisMonth .' -1 day'));
echo 'last day of prev month: '.$lastDayOfPrevMonth.'<br><br>';
$firstDayOfPrevMonth = date('Y-m-d', strtotime($firstDayOfThisMonth .' -1 month'));
echo 'first day of prev month: '.$firstDayOfPrevMonth.'<br><br>';
echo '------------------------------------<br><br>';
**
测试结果
**
now: 2021-03-30
first day of this month: 2021-03-01
last day of prev month: 2021-02-28
first day of prev month: 2021-02-01
now: 2021-10-01
first day of this month: 2021-10-01
last day of prev month: 2021-09-30
first day of prev month: 2021-09-01
now: 2021-12-23
first day of this month: 2021-12-01
last day of prev month: 2021-11-30
first day of prev month: 2021-11-01
上一篇: Day09_04目标:File类的创建和删除的方法
下一篇: Java创建和删除文件