且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在php中获取周末日期

更新时间:2023-11-29 10:57:04

如果您使用较旧的PHP安装(或没有DateTime类):

If you're using an older installation of PHP (or don't have the DateTime class):

$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");
}

我们循环遍历日期范围,并检查一天是否为 0 6 索引(星期日或星期六)。

We loop through the date range and check to see if the day is a 0 or 6 index (Sunday or Saturday).