关于php:PHP-获取前月-月初月末-日期

35次阅读

共计 1740 个字符,预计需要花费 5 分钟才能阅读完成。


$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


正文完
 0