下面的代码。
strtotime("first saturday", strtotime("+2 month"));
工作正常,但在 April +2 month
、 October+ 8 month
和 December 月份+ 10 month
给出的是该月的第二个星期六而不是第一个。
任何想法是什么导致它以及如何阻止它。
奇妙
$saturday = strtotime("first saturday", strtotime("+2 month", strtotime(date("01-m-Y"))));
这是因为“第一个星期六”是从给定日期计算的。如果给定日期已经是星期六,则计算下一个日期。
如果您需要特定月份的第一个星期六,请执行以下操作:
$stamp = time();
$tm = localtime($stamp, TRUE);
// +1 to account for the offset, +2 for "+2 month"
$begin = mktime(0, 0, 0, $tm['tm_mon'] + 1 + 2, 1, 1900 + $tm['tm_year']);
if (6 == $begin['tm_wday']) {
// we already got the saturday
$first_saturday = $stamp;
} else {
$first_saturday = strtotime('first saturday', $begin);
}
使用当月的第一天创建您的原始时间戳,然后只需检查月/年:
function firstSaturday($month, $year) {
if (!is_numeric($month) && !is_numeric($year)) {
return -1;
}
return strtotime('first saturday', mktime(0, 0, 0, $month, 1, $year));
}