日期范围阵列,不包括星期日&在PHP度假 [英] Date range array excluding the Sunday & the holiday in PHP
问题描述
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中执行此操作?
我尝试了一些但找不到正确的方法。
对于星期日部分: / p>
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 [] = $ current);
$ current = strtotime($ step,$ current);
}
return $ dates;
}
假期部分:
首先,您需要将日期加载到某种数组中,然后循环遍历每个日期的数组,并检查它们是否匹配。
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,
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 ? 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.
这篇关于日期范围阵列,不包括星期日&在PHP度假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!