开发者

How do I calculate the days per month given a start date and a duration?

Trying to figure out a small problem. I have a start开发者_运维技巧 date and a duration (in weeks) and need to calculate the number of work days per month over the duration.

For example: Start Date: 2011-02-07 Duration: 10 weeks

I'd like to get back the following:

Feb: 16 days March: 23 days April: 11 days

Any help would be great. Thanks.


Pre 5.3 solution:

$start = $current = strtotime('2011-02-07');
$end = strtotime('+10 weeks', $start);
$months = array();
while($current < $end) {
    $month = date('M', $current);
    if (!isset($months[$month])) {
        $months[$month] = 0;
    }
    $months[$month]++;
    $current = strtotime('+1 weekday', $current);
}
print_r($months);

Output (codepad):

Array
(
    [Feb] => 16
    [Mar] => 23
    [Apr] => 11
)


$start=date_create('2011-02-07');
$interval=new DateInterval('P10W');
$end=date_add(clone $start,$interval);//use clone otherwise $start gets changed

$period=new DatePeriod($start, new DateInterval('P1D'), $end);

foreach($period as $day){
  if($day->format('N')<6) $workdays[$day->format('F')]++; //N is 1-7 where Monday=1
}

var_dump($workdays);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜