【问题标题】:Date range array excluding the Sunday & the holiday in PHP日期范围数组,不包括 PHP 中的星期日和节假日
【发布时间】:2012-05-15 06:49:51
【问题描述】:

我有一个函数可以返回数组中两个日期之间的所有日期,但我需要在该数组中排除星期日。

public function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { 
    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);
    while( $current <= $last ) { 
        $dates[] = date($format, $current);
        $current = strtotime($step, $current);
    }
    return $dates;
}

排除星期日后,我有一个表格,我将在其中存储一些日期,我也需要从数组中排除这些日期。

例如,如果我输入的日期范围为 01-05-2012(DD-MM-YYYY) 到 10-05-2012, 06-05-2012 将是星期日,日期 01-05-2012 和 08-05-2012 将在我上面提到的表格中, 最终的输出应该是这样的,

02-05-2012
03-05-2012
04-05-2012
05-05-2012
07-05-2012
09-05-2012
10-05-2012

如何在 PHP 中做到这一点? 我尝试了一些,但找不到正确的方法。

【问题讨论】:

    标签: php date date-range


    【解决方案1】:

    周日部分:

    public function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { 
        $dates = array();
        $current = strtotime($first);
        $last = strtotime($last);
        while( $current <= $last ) { 
            if (date("D", $current) != "Sun")
                $dates[] = date($format, $current);
            $current = strtotime($step, $current);
        }
        return $dates;
    }
    

    假期部分:

    首先,您需要将日期加载到某种数组中,然后循环遍历每个日期的数组并检查它们是否匹配。

    【讨论】:

    • 我会使用 date('w', $current) != 0
    • 我也修复了假期部分 :)
    • 你可以发布你的解决方案以防其他人有同样的问题,否则他们只会在这里看到问题:)
    【解决方案2】:

    我找到了问题的答案,感谢帮助我的人。

    public function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { 
        $dates = array();
        $current = strtotime($first);
        $last = strtotime($last);
        while( $current <= $last ) {
            $sql = "SELECT * FROM ost_holidays where holiday_date='".date('Y-m-d', $current)."' LIMIT 1";
            $sql = db_query($sql);
            $sql = db_fetch_array($sql);
            if($sql['holiday_date'] != date('Y-m-d',$current))
                if (date('w', $current) != 0)
                $dates[] = date($format, $current);
                $current = strtotime($step, $current);
        }
        return $dates;
    }
    

    以上代码用于删除给定范围内的节假日和星期日。

    【讨论】:

      【解决方案3】:

      我在 Jquery 中做了同样的上述方法

      //Convert dates into desired formatt
       function convertDates(str) {
           var date = new Date(str),
               mnth = ("0" + (date.getMonth() + 1)).slice(-2),
               day = ("0" + date.getDate()).slice(-2);
           return [date.getFullYear(), mnth, day].join("-");
       }
      
       // Returns an array of dates between the two dates
       var getDates = function(startDate, endDate, holidays) {
           var dates = [],
               currentDate = startDate,
               addDays = function(days) {
                   var date = new Date(this.valueOf());
                   date.setDate(date.getDate() + days);
                   return date;
               };
           while (currentDate <= endDate) {
               dates.push(currentDate);
               currentDate = addDays.call(currentDate, 1);
           }
           return dates;
       };
       //Indise Some Function
       var datesTemp = [];
       var dates = getDates(new Date(prodDet.details.date1), new Date(prodDet.details.date2));
       dates.forEach(function(date) {
           if (date.getDay() != 0) {
               datesTemp.push(convertDates(date));
           }
       });
       datesTemp.forEach(function(date) {
           for (var j = 0; j < prodDet.holidays.length; j++) {
               if ((prodDet.holidays[j] != date)) {
                   ideal.idates.push(date);
               }
           }
       });
       console.log(ideal.idates);
       //Function Ends Here
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-06
        • 1970-01-01
        • 1970-01-01
        • 2019-04-08
        • 1970-01-01
        • 1970-01-01
        • 2015-10-25
        • 2019-10-03
        相关资源
        最近更新 更多