php日期函数和计算星期几,以确保周末被跳过

时间:2013-05-01 16:53:57

标签: php sql

我昨天设置了这个旋转日历,我想我可能会遇到问题,而且今天确实发生了我预期的问题。日期改变时,日期发生了变化。而不是像昨天一样跳过周末,而是周六而不是周一。它只能假设显示周一至周五,而不是+3,再回到周一至周五。示例代码:

echo "Today";
echo date('m/d'); 
echo "<br>";
echo substr(date('l/m/d', strtotime('+1 day')), 0, 2). date('m/d', strtotime('+1 day')); 
echo "<br>";
echo substr(date('l/m/d', strtotime('+2 day')), 0, 1). date('m/d', strtotime('+2 day'));
echo "<br>";
echo substr(date('l/m/d', strtotime('+5 day')), 0, 1). date('m/d', strtotime('+5 day'));
echo "<br>";
echo substr(date('l/m/d', strtotime('+6 day')), 0, 1). date('m/d', strtotime('+6 day'));
echo "<br>";
echo substr(date('l/m/d', strtotime('+7 day')), 0, 1). date('m/d', strtotime('+7 day'));
echo "<br>";
echo substr(date('l/m/d', strtotime('+8 day')), 0, 2). date('m/d', strtotime('+8 day'));
echo "<br>";
echo substr(date('l/m/d', strtotime('+9 day')), 0, 1). date('m/d', strtotime('+9 day'));
echo "<br>";
echo substr(date('l/m/d', strtotime('+12 day')), 0, 1). date('m/d', strtotime('+12 day'));
echo "<br>";
echo substr(date('l/m/d', strtotime('+13 day')), 0, 1). date('m/d', strtotime('+13 day'));

显然模式没有完成工作,我怎样才能确保总是跳过周末?

此外,我将此结论为sql查询,如下所示:(实际代码)

sum(case when cast(a.follow_up as date)=cast(GETDATE() as date) then 1 else 0 end) 'Today<br>&nbsp;" . date('m/d') . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='1' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+1 day')), 0, 2).'<br>' . date('m/d', strtotime('+1 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='2' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+2 day')), 0, 1).'<br>' . date('m/d', strtotime('+2 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='3' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+5 day')), 0, 1).'<br>' . date('m/d', strtotime('+5 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='4' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+6 day')), 0, 1).'<br>' . date('m/d', strtotime('+6 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='5' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+7 day')), 0, 1).'<br>' . date('m/d', strtotime('+7 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='6' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+8 day')), 0, 2).'<br>' . date('m/d', strtotime('+8 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='7' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+9 day')), 0, 1).'<br>' . date('m/d', strtotime('+9 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='8' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+12 day')), 0, 1).'<br>' . date('m/d', strtotime('+12 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='9' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+13 day')), 0, 1).'<br>' . date('m/d', strtotime('+13 day')) . "',

1 个答案:

答案 0 :(得分:1)

日期(&#39; N&#39;)将以1周一到7周日的格式给出星期几 因此,如果我们减去1得到0-6,那么使用它来回溯几天,它将把我们带回到上周一。

即星期一= 1,1-1 = 0,-0days是星期一    星期二= 2,2-1 = 1,-1天是星期一 ..
   星期五= 5,5-1 = 4-4天是星期一

$lastmonday=strtotime("-".(date("N")-1)." days");
for($loop=0;$loop<14;$loop++)
{
    $theday=strtotime("+".$loop." days", $lastmonday);
    if(date("N", $theday)>5)
    {
       echo 'weekend';
    }
    else
    {
        echo date('D m/d'); 
    }
    echo "<br>";
}