看来您对“最后”在这里的实际含义理解错误。
这与该月的最后一天或最后一周无关 - 它是“最后”,如“最近的前一个”。
你可以从类似的事情开始strtotime('last day of 2020-03')
- 那会给你31 Mar 2020
,这是星期二。
如果你那么check这是一周中的哪一天,可能是星期六或星期日 -then你可以走了last friday
再次使用该值,以降落在该月的最后一个工作日。
Edit:
2020 年所有月份的示例
for($i=1; $i<13; ++$i) {
$year_month = '2020-'.$i;
$last_of_month = strtotime('last day of '.$year_month);
$weekday = date('N', $last_of_month);
if($weekday < 6) {
echo 'last weekday of ', $year_month, ' is ', date('Y-m-d, l', $last_of_month), "<br>\n";
}
else {
$last_weekday_of_month = strtotime('last friday', $last_of_month);
echo 'last day of ', $year_month, ' is ', date('Y-m-d, l', $last_of_month), ' - last weekday of ', $year_month, ' is ', date('Y-m-d, l', $last_weekday_of_month), "<br>\n";
}
}
这将为您提供以下输出:
last weekday of 2020-1 is 2020-01-31, Friday
last day of 2020-2 is 2020-02-29, Saturday - last weekday of 2020-2 is 2020-02-28, Friday
last weekday of 2020-3 is 2020-03-31, Tuesday
last weekday of 2020-4 is 2020-04-30, Thursday
last day of 2020-5 is 2020-05-31, Sunday - last weekday of 2020-5 is 2020-05-29, Friday
last weekday of 2020-6 is 2020-06-30, Tuesday
last weekday of 2020-7 is 2020-07-31, Friday
last weekday of 2020-8 is 2020-08-31, Monday
last weekday of 2020-9 is 2020-09-30, Wednesday
last day of 2020-10 is 2020-10-31, Saturday - last weekday of 2020-10 is 2020-10-30, Friday
last weekday of 2020-11 is 2020-11-30, Monday
last weekday of 2020-12 is 2020-12-31, Thursday