当前日期减去4个月?

2022-08-30 23:59:19

我有这种格式的日期(YYYYMM):

201201  // Gen, 2012
201202  // Feb, 2012
201203  // ecc

假设从201203我想减去4个月。我不能做,因为它是=201203 - 4201199

201203 - 4应输出(2011 年 11 月)201111

也许我应该将我的字符串转换为日期,然后将其传递给-4个月的strtotime?

有什么建议吗?


答案 1

你可以在 PHP 中使用 strtotime

$date = strtotime('2012-05-01 -4 months');

本文将为您提供帮助。


答案 2

strtotime()可以做到这一点,但你需要添加一个月的某一天,以便它解析日期:

$input = '201203';

$input .= '01';

$date = strtotime($input .' -4 months');

echo date('Ym', $date);

2011年11月产出:

201111

推荐