日期范围阵列,不包括星期日&在PHP度假 [英] Date range array excluding the Sunday & the holiday in PHP

查看:73
本文介绍了日期范围阵列,不包括星期日&在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.

这篇关于日期范围阵列,不包括星期日&amp;在PHP度假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆