PHP strtotime不包括元旦...如何包括假期?
PHP strtotime excludes New Year's Day... How to include the holiday?
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:
Observe the following remarks when the current day-of-week is the same
as the day-of-week used in the date/time string:
[...]
"ordinal dayname" does advance to another day. (Example "first wednesday july 23rd, 2008" means "2008-07-30").
[...]
"ordinal dayname 'of' " does not advance to another day. (Example: "first tuesday of july 2008" means "2008-07-01").
你可以试试这个。
echo date("D Y-m-d", strtotime("First Wednesday of January 2020"));
// Wed 2020-01-01
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:
Observe the following remarks when the current day-of-week is the same as the day-of-week used in the date/time string:
[...]
"ordinal dayname" does advance to another day. (Example "first wednesday july 23rd, 2008" means "2008-07-30").
[...]
"ordinal dayname 'of' " does not advance to another day. (Example: "first tuesday of july 2008" means "2008-07-01").
你可以试试这个。
echo date("D Y-m-d", strtotime("First Wednesday of January 2020"));
// Wed 2020-01-01