-->

日期范围阵列不包括周日和PHP中的节日(Date range array excluding the

2019-09-17 06:10发布

我有所有返回两个日期之间的日期在一个阵列的功能,但我需要排除周日数组英寸

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;
}

除星期天之后,我有,我会存储一些日期的表,我需要排除从阵列中的日期了。

喜欢,如果我输入的日期范围为2012年1月5日(DD-MM-YYYY)至2012-05-10,在2012年6月5日是星期天与日期2012年1月5日和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? 我尝试了一些,但找不到这样做的正确方法。

Answer 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;
}

对于节假日部分:

首先,你需要通过数组为每个日期的日期加载到某种数组,然后循环,并检查它们是否匹配。



Answer 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;
}

上面的代码是用于在给定的范围内除去假日&的星期日。



Answer 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


文章来源: Date range array excluding the Sunday & the holiday in PHP