日期范围数组,不包括星期日和;PHP中的假日
我有一个函数,它返回数组中两个日期之间的所有日期,但我需要在该数组中排除星期天日期范围数组,不包括星期日和;PHP中的假日,php,date,date-range,Php,Date,Date Range,我有一个函数,它返回数组中两个日期之间的所有日期,但我需要在该数组中排除星期天 public function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { $dates = array(); $current = strtotime($first); $last = strtotime($last); while( $current <= $last ) {
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;
}
如何在PHP中实现这一点?
我试了一些,但找不到正确的方法。周日部分:
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;
}
公共函数日期范围($first,$last,$step='+1天',$format='d/m/Y'){
$dates=array();
$current=strottime($first);
$last=strottime$last;
虽然($current我找到了我问题的答案,但感谢帮助我的人
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;
}
公共函数日期范围($first,$last,$step='+1天',$format='d/m/Y'){
$dates=array();
$current=strottime($first);
$last=strottime$last;
而($current我在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
//将日期转换为所需格式
函数转换日期(str){
var日期=新日期(str),
mnth=(“0”+(date.getMonth()+1)).slice(-2),
day=(“0”+date.getDate()).slice(-2);
return[date.getFullYear(),mnth,day].join(“-”;
}
//返回两个日期之间的日期数组
var getDates=函数(开始日期、结束日期、假日){
var日期=[],
当前日期=开始日期,
addDays=函数(天){
var date=新日期(this.valueOf());
date.setDate(date.getDate()+天);
返回日期;
};
虽然(currentDate我会使用date('w',$current)!=0I在这里发布问题后得到了@jimpic的解决方案,但我认为解决方案相当好,无论如何,感谢你们两位,但我对假期部分相当困惑。我也修复了假期部分:)如果其他人有相同的问题,您可以发布您的解决方案,否则他们只会在此处看到问题:)
//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