/

PHP 月份加减问题

看现象

var_dump(date("Y-m-d", strtotime("+1 month", strtotime("2020-07-31"))));
// string(10) "2020-08-31" 符合预期

var_dump(date("Y-m-d", strtotime("+1 month", strtotime("2020-05-31"))));
// string(10) "2020-07-01" 不符合预期,预期 2020-06-30

var_dump(date("Y-m-d", strtotime("-1 month", strtotime("2020-02-29"))));
// string(10) "2020-01-29" 符合预期

var_dump(date("Y-m-d", strtotime("-1 month", strtotime("2020-03-31"))));
// string(10) "2020-03-02" 不符合预期,预期 2020-02-29


// Carbon\Carbon
Carbon::parse("2020-07-31")->addMonth()->toDateString();
// "2020-08-31"
Carbon::parse("2020-05-31")->addMonth()->toDateString();
// "2020-07-01"
Carbon::parse("2020-02-29")->subMonth()->toDateString();
// "2020-01-29"
Carbon::parse("2020-03-31")->subMonth()->toDateString();
// "2020-03-02"

// 结果与 strtotime 一致。

原因

var_dump(date("Y-m-d", strtotime("+1 month", strtotime("2020-05-31"))));
// string(10) "2020-07-01"

date 内部的处理逻辑:

  1. 2020-05-31+1 month 也就是 2020-06-31
  2. 再做日期规范化,因为没有 06-31,所以 06-31 就等于了 07-01
var_dump(date("Y-m-d", strtotime("2020-06-31")));
// string(10) "2017-07-01"

var_dump(date("Y-m-d", strtotime("next month", strtotime("2017-01-31"))));
// string(10) "2017-03-03"

var_dump(date("Y-m-d", strtotime("last month", strtotime("2017-03-31"))));
// string(10) "2017-03-03"

解决方案

var_dump(date("Y-m-d", strtotime("last day of -1 month", strtotime("2017-03-31"))));
// string(10) "2017-02-28"

var_dump(date("Y-m-d", strtotime("first day of +1 month", strtotime("2017-08-31"))));
// string(10) "2017-09-01"

// 但要注意短语的含义:
var_dump(date("Y-m-d", strtotime("last day of -1 month", strtotime("2017-03-01"))));
// string(10) "2017-02-28"

如果使用 Carbon\Carbon 可以用 subMonthNoOverflowaddMonthNoOverflow 防止进位:

Carbon::parse('2020-03-31')->subMonthNoOverflow()->toDateString();
// "2020-02-29"

Carbon::parse("2020-05-31")->addMonthNoOverflow()->toDateString();
// "2020-06-30"

Ym 类似问题

在当日是 31 号场景下:

Carbon::createFromFormat('Ym', '202206')->format('Y-m');
// 结果是 2022-07 不符合本意

! Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to zero-like values ( 0 for hour, minute, second and fraction, 1 for month and day, 1970 for year and UTC for timezone information)

Without !, all fields will be set to the current date and time.

如果 format 包含字符 !,则未在 format 中提供的生成日期/时间部分以及 ! 左侧的值将设置为 Unix 纪元的相应值。

The Unix epoch is 1970-01-01 00:00:00 UTC.

Carbon::createFromFormat('!Ym', '202206')->format('Y-m');
// 结果是 2022-06
Carbon::createFromFormat('Ym|', '202206')->format('Y-m');
// 结果是 2022-06

$format = 'Y-m-!d H:i:s';
$date = DateTimeImmutable::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
// Format: Y-m-!d H:i:s; 1970-01-15 15:16:17

References

– EOF –