当我生成日期时,如何排除星期六和星期日

时间:2014-01-13 06:38:53

标签: php date datetime

我正在使用我的代码生成日期我想排除星期日和星期六请在这里查看我的代码

for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {

$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date")); 
if ($from < $start_date)
    $from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week"));   
if ($to > $end_date) {
    $to = $end_date;
}
if ($from <= $to) {
    array_push($weekfrom, $from);
    array_push($weekto, $to);
}

$n = count($weekfrom);

for ($i = 0; $i < $n; $i++) {
    echo $weekfrom[$i];
}}

2 个答案:

答案 0 :(得分:1)

你可以这样做。

$getDate = date('l', strtotime($date));
if ($getDate != 'Saturday' AND $getDate != 'Sunday') {
    ......
}

如果该日期不是星期六或星期日,则处理该事物。

答案 1 :(得分:0)

只需将其添加到循环开头:

if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;

像这样:

for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {

if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;

$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date")); //Returns the date of monday in week
if ($from < $start_date)
$from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week")); //Returns the date of sunday in week
if ($to > $end_date) {
$to = $end_date;
}
if ($from <= $to) {
array_push($weekfrom, $from);
array_push($weekto, $to);
}

$n = count($weekfrom);

for ($i = 0; $i < $n; $i++) {
echo $weekfrom[$i];
}}