要获取 2023-07 的上一个月,可以使用 strtotime 函数和 date 函数来进行计算。具体代码如下:

$prevMonth = date('Y-m', strtotime('2023-07-01 -1 month'));
echo $prevMonth; // 输出:2023-06

要获取去年的这个月,可以使用 date 函数和 strtotime 函数来进行计算。具体代码如下:

$lastYearThisMonth = date('Y-m', strtotime('2023-07-01 -1 year'));
echo $lastYearThisMonth; // 输出:2022-07

以上代码中,strtotime('2018-07-01 -1 month') 表示将日期字符串 2018-07-01 减去一个月,得到上一个月的日期。strtotime('2018-07-01 -1 year') 表示将日期字符串 2018-07-01 减去一年,得到去年的日期。最后使用 date 函数将计算后的日期格式化为 Y-m 的格式。


原文地址: https://www.cveoy.top/t/topic/mLl8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录