从输出中排除从星期六开始的周数

时间:2015-12-14 12:11:04

标签: php

我有这个脚本,它显示一个月内每周结束日期的开始日期。

我想以某种方式过滤出可能从星期六开始的第一周..例如,如果我们以2015年8月为例,第一周

<?php       
$textdt="01 Aug 2015";
$dt= strtotime( $textdt);
$currdt=$dt;
$nextmonth=strtotime($textdt."+1 month");
$i=0;
do 
{
    $weekday= date("w",$currdt);
    $nextday=7-$weekday;
    $endday=abs($weekday-7);
    $startarr[$i]=$currdt;
    $endarr[$i]=strtotime(date("Y-m-d",$currdt)."+$endday day");
    $currdt=strtotime(date("Y-m-d",$endarr[$i])."+1 day");
    echo "Week ".($i+1)." - ". date("d-m-Y",$startarr[$i])." to ". date("d-m-Y",$endarr[$i])."<br>";
     $i++;

}while($endarr[$i-1]<$nextmonth);
?>

当前输出

Week 1 - 01-08-2015 to 02-08-2015
Week 2 - 03-08-2015 to 09-08-2015
Week 3 - 10-08-2015 to 16-08-2015
Week 4 - 17-08-2015 to 23-08-2015
Week 5 - 24-08-2015 to 30-08-2015
Week 6 - 31-08-2015 to 06-09-2015

所需的输出

Week 1 - 03-08-2015 to 09-08-2015
Week 2 - 10-08-2015 to 16-08-2015
Week 3 - 17-08-2015 to 23-08-2015
Week 4 - 24-08-2015 to 30-08-2015
Week 5 - 31-08-2015 to 06-09-2015

2 个答案:

答案 0 :(得分:2)

只需检查date("D",$startarr[$i]) !== "Sat",或者您可以使用if($weekday != 6)

if(date("D",$startarr[$i]) !== "Sat") {
    echo "Week ".($i+1)." - ". date("d-m-Y",$startarr[$i])." to ". date("d-m-Y",$endarr[$i])."<br>";
}

答案 1 :(得分:1)

使用if

<?php       
$textdt="01 Aug 2015";
$dt= strtotime( $textdt);
$currdt=$dt;
$nextmonth=strtotime($textdt."+1 month");
$i=0;
$weekIndex = 1;
do 
{
    $weekday= date("w",$currdt);
    if($weekday != 6) {
        $nextday=7-$weekday;
        $endday=abs($weekday-7);
        $startarr[$i]=$currdt;
        $endarr[$i]=strtotime(date("Y-m-d",$currdt)."+$endday day");
        $currdt=strtotime(date("Y-m-d",$endarr[$i])."+1 day");
        echo "Week ".($weekIndex++)." - ". date("d-m-Y",$startarr[$i])." to ". date("d-m-Y",$endarr[$i])."<br>";
    }
     $i++;

}while($endarr[$i-1]<$nextmonth);
?>

修改:已添加$weekIndex