问题描述
看起来PHP的strtotime函数正在从结果中排除“元旦”。有没有办法强制strtotime包含它?其他假期(例如劳动节)似乎没有这个问题(请参见下面的第3个代码示例)。
谢谢!
// The 1st Wed in Jan 2020 falls on New Year's Day
// But we get: "Wed 2020-01-08" (INCORRECT... it should return "Wed 2020-01-01")
echo date("D Y-m-d",strtotime("First Wednesday " . "2020-01"));
// However,asking for the 1st Thu in Jan 2020 returns the correct result: "Thu 2020-01-02"
echo date("D Y-m-d",strtotime("First Thursday " . "2020-01"));
// Even asking for the 1st Mon in Sep 2020,which is Labor Day returns the correct result: "Mon 2020-09-07"
echo date("D Y-m-d",strtotime("First Monday " . "2020-09"));
解决方法
您应该改用"First Wednesday of 2020-01"
(注意of
)。
每the docs:
,当当前星期几相同时,请注意以下几点 作为日期/时间字符串中使用的星期几:
[...]
“常规日期名称” 会改到另一天。 (示例“ 2008年7月23日,星期三”表示“ 2008-07-30”。)
[...]
“'的普通日名'” 不会提前到另一天。 (例如:“ 2008年7月第一个星期二”表示“ 2008-07-01”)。
您可以尝试这个。
echo date("D Y-m-d",strtotime("First Wednesday of January 2020"));
// Wed 2020-01-01