[英]Date range array excluding the Sunday & the holiday in PHP
I have a function which returns all the dates between two dates in an array, But I need to exclude Sundays in that 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;
}
After excluding the Sundays, I have a table where I will be storing some dates, I need to exclude those dates from the array too. 在排除星期日之后,我有了一个要存储一些日期的表,我也需要从数组中排除这些日期。
like, If I enter the date range as 01-05-2012(DD-MM-YYYY) to 10-05-2012, The 06-05-2012 will be Sunday & the date 01-05-2012 & 08-05-2012 will be in the table which I mentioned above, The final out put should be like, 例如,如果我输入日期范围为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
How to do this in PHP ? 如何在PHP中做到这一点? I tried some but couldn't find the right way to do it.
我尝试了一些,但找不到正确的方法。
For the Sundays part: 对于星期日部分:
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;
}
For the holidays part: 对于假期部分:
First you need to load the dates into some kind of array and then loop through the array for each of your dates and check if they match. 首先,您需要将日期加载到某种数组中,然后为每个日期循环遍历数组,并检查它们是否匹配。
I found the answer for my question, Thanks for the people who helped me. 我找到了问题的答案,谢谢帮助我的人们。
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;
}
The above code is for removing the holidays & the Sundays in the given range. 上面的代码用于删除给定范围内的假日和星期日。
I did this same above method in Jquery 我在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.