我正在开发一个php应用程序,它要求我提取两个日期之间的周末日期,然后将它们作为单个记录插入mysql数据库中.
我在想是否有更简单的方法,而不是通过开始日期和结束日期之间的循环,并检查每个日期检查是否date('l', strtotime($date))返回"星期六"或"星期日"
谢谢你的时间
苏尼尔
Nic*_*sta 12
如果您使用的是较旧的PHP安装(或者没有DateTime类):
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
while (date("Y-m-d", $now) != date("Y-m-d", $end_date)) {
$day_index = date("w", $now);
if ($day_index == 0 || $day_index == 6) {
// Print or store the weekends here
}
$now = strtotime(date("Y-m-d", $now) . "+1 day");
}
Run Code Online (Sandbox Code Playgroud)
我们遍历日期范围和检查,看看是否这一天是一个0或6指数(星期日或星期六).