从今天起X天后计算

时间:2009-07-01 05:51:03

标签: php

我正在开发一个网页,因为我需要计算指定日期的x天,问题是我们排除了星期六和星期日。例如$ Shipdate = '06 / 30/2009',x是5表示,我想答案'7'[即30是星期二所以5天之后它将是星期日,所以有两个假期(星期六和星期日)所以我们加5 + 2(星期六和星期日)] = 7。请帮我看看,提前致谢。

3 个答案:

答案 0 :(得分:1)

通常,您需要能够指定排除重要日期的日历。考虑圣诞节或公众假期。 This似乎是考虑公共假期的代码,您需要对其进行修改或将其与您的假期一起参数化。

答案 1 :(得分:0)

<?php

// ** Set Beginning and Ending Dates, in YYYY-mm-dd format **
 $begin = '2008-01-01';
 $end = '2008-03-31';
 $begin_mk = strtotime("$begin"); 
 $end_mk = strtotime("$end");

// ** Calculate number of Calendar Days between the two dates
 $datediff = ( $end_mk > $begin_mk ? ( $end_mk - $begin_mk ) : ( $begin_mk - $end_mk ) );
 $days = ( ( $datediff / 3600 ) / 24 );
  $days = $days + 1; // to be inclusive of last date;

// ** Count days excluding Sundays **
  $iteration = 0; 
  $numDaysExSunday = 0;
for ($i=1; $i<=$days; $i++) {
  $weekday = date("w", strtotime("$begin + $iteration day"));
       echo "$weekday<br>";
     **// i change only this line to add saturday**
     if ($weekday !== '0' && $weekday !== '6') {
        $numDaysExSunday++;
     }
  $iteration++; 
}

// ** Output number of days excluding Sundays **
echo $numDaysExSunday;
?>

我从

拿走它

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23499410.html

解决方案排除星期日但需要只更改一行以排除星期六我将其写入代码

答案 2 :(得分:0)

function Weekdays($start,$end){
    $days=0;
    if ( $end <= $start ) {
        // This is invalid data.
        // You may consider zero to be valid, up to you.
        return; // Or throw an error, whatever.
    }
    while($start<$end){
        $dayofweek = date('w',$start);
        if( 6!= $dayofweek && 0 != $dayofweek){
            $days++;
        }
        $start = strtotime('+1 day',$start);
    }
    return $days;
}

您可能需要稍微调整一下,具体取决于您是想将其计为一天还是零,如果开头是与结束相同的那一天。