取消周末之间的两个日期之间的日期范围 [英] Get date range between two dates excluding weekends

查看:98
本文介绍了取消周末之间的两个日期之间的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下日期:

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) 

不包括周末日。

不,这不是功课...由于某种原因,我今天不能直视

No, it's not homework...for some reason I just can't think straight today.

推荐答案

对于PHP> = 5.3.0,请使用 DatePeriod 类。不幸的是几乎没有记录。

For PHP >= 5.3.0, use the DatePeriod class. It's unfortunately barely documented.

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

这篇关于取消周末之间的两个日期之间的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆