【问题标题】:Get date range between two dates excluding weekends获取两个日期之间的日期范围,不包括周末
【发布时间】:2011-03-22 03:07:19
【问题描述】:

鉴于以下日期:

6/30/2010 - 7/6/2010

还有一个静态变量:

$h = 7.5

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

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) 

周末除外。

不,这不是家庭作业...由于某种原因,我今天无法思考。

【问题讨论】:

  • 枚举日期,使用getdate(iter_date)["wday"] 跳过周末(工作日:0 或 6)。

标签: php arrays date


【解决方案1】:

对于 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);

【讨论】:

  • 当我运行这个我得到 Array ( [2010-06-30] => 7.5 [2010-07-01] => 7.5 [2010-07-02] => 7.5 [2010-07 -05] => 7.5 ) 最后一天不见了,7/6。我错过了一些简单的东西吗?
  • 好像改变 $end = '7/6/2010'; to $end = date ( 'm/d/Y' , strtotime ( '+1 day' , strtotime ('7/06/2010') ) );得到我正在寻找的东西。
  • 是的,DatePeriod 似乎在结束日期之前停止。我的错,已修复。
  • 如果我们现在没有结束日期,如何获取不包括周末的 n 天的日期范围。
【解决方案2】:

最简单的方法:

$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);

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

【讨论】:

  • 注意夏令时
  • 很好的答案,对于没有夏令时的地方:)
【解决方案3】:

这是 gnud 的答案,但作为一个函数(还添加了一个从计算中排除当前日期的选项):

(以下示例)

public function getNumberOfDays($startDate, $endDate, $hoursPerDay="7.5", $excludeToday=true)
{
    // d/m/Y
    $start = new DateTime($startDate);
    $end = new DateTime($endDate);
    $oneday = new DateInterval("P1D");

    $days = array();

    /* 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")] = $hoursPerDay;
        } 
    }    

    if ($excludeToday)
        array_pop ($days);

    return $days;       
}

并使用它:

$date1 = "2012-01-12";
$date2 = date('Y-m-d'); //today's date  

$daysArray = getNumberOfDays($date1, $date2);

echo 'hours: ' . array_sum($daysArray);
echo 'days: ' . count($daysArray);

【讨论】:

  • 你好@jarrod,我也想要几分钟。
【解决方案4】:

这是 OOP 方法,以防万一。它返回一个包含所有日期的数组,周末除外。

    class Date{
        public function getIntervalBetweenTwoDates($startDate, $endDate){
            $period = new DatePeriod(
                 new DateTime($startDate),
                 new DateInterval('P1D'),
                 new DateTime($endDate)
            );
            $all_days = array();$i = 0;
            foreach($period as $date) {
                if ($this->isWeekend($date->format('Y-m-d'))){
                    $all_days[$i] = $date->format('Y-m-d');
                    $i++;
                }
            }
            return $all_days;
        }
        public function isWeekend($date) {
            $weekDay = date('w', strtotime($date));
            if (($weekDay == 0 || $weekDay == 6)){
                return false;
            }else{
                return true;
            }
        }
    }
    $d = new Date();
    var_dump($d->getIntervalBetweenTwoDates('2015-08-01','2015-08-08'));

【讨论】:

  • 如果你有两个相同的日期怎么办? var_dump($d->getIntervalBetweenTwoDates('2015-08-01','2015-08-01'))
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
  • 2019-04-15
相关资源
最近更新 更多