2012-05-15 90 views
2

我有一个返回数组中两个日期之间的所有日期的函数,但是我需要在该数组中排除星期日。排除星期日和节假日的日期范围array

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日& 2012年8月5日将在我上面提到的, 最后出来放应该像表,

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

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

回答

2

对于周日部分:

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

对于假期部分:

首先,你需要通过数组为每个日期和支票的日期加载到某些类型的数组,然后循环如果他们匹配。

+1

我会用 日期( 'W',$电流)!= 0 – h00ligan

+0

权利,那最好不过了 – jimpic

+0

我得到了@jimpic的解决方案后,在这里发布这个问题,但我认为解决方案是更好,反正感谢你们两个,但我很困惑与假期的一部分。 –

0

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

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

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

-1

我这样做同样的上述方法在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