获取两个日期之间的日期范围,不包括周末

Jas*_*son 9 php arrays date

鉴于以下日期:

6/30/2010 - 7/6/2010
Run Code Online (Sandbox Code Playgroud)

和一个静态变量:

$h = 7.5
Run Code Online (Sandbox Code Playgroud)

我需要创建一个数组,如:

Array ( [2010-06-30] => 7.5 [2010-07-01] => 7.5 => [2010-07-02] => 7.5 => [2010-07-05] => 7.5 => [2010-07-06] => 7.5) 
Run Code Online (Sandbox Code Playgroud)

周末天被排除在外.

不,这不是作业......出于某种原因,我今天无法直接思考.

gnu*_*nud 24

对于PHP> = 5.3.0,请使用DatePeriod类.遗憾的是,它几乎没有记录.

$start = new DateTime('6/30/2010');
$end = new DateTime('7/6/2010');
$oneday = new DateInterval("P1D");

$days = array();
$data = "7.5";

/* Iterate from $start up to $end+1 day, one day in each iteration.
   We add one day to the $end date, because the DatePeriod only iterates up to,
   not including, the end date. */
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
    $day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
    if($day_num < 6) { /* weekday */
        $days[$day->format("Y-m-d")] = $data;
    } 
}    
print_r($days);
Run Code Online (Sandbox Code Playgroud)


Ste*_*hen 5

最简单的方法:

$start = strtotime('6/30/2010');
$end = strtotime('7/6/2010');
$result = array();
while ($start <= $end) {
    if (date('N', $start) <= 5) {
        $current = date('m/d/Y', $start);
        $result[$current] = 7.5;
    }
    $start += 86400;
}
print_r($result);
Run Code Online (Sandbox Code Playgroud)

更新: 忘记跳过周末。现在应该可以了。