欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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


相关标签: PHP php 日期