PHP函数不会返回10个工作日

时间:2015-05-19 17:19:05

标签: php function

我有以下代码来计算从现在开始的10个工作日,此代码是根据之前已回答显示3个工作日的答案进行修改的,但是我想假设从现在起10个工作日,但它不会返回正确的日期我请求一些帮助

function working_days($days) {
    $count = 0;
$day = strtotime('now');
while ($count < 10 || date('N', $day) > 5) {
   $count++;
   $day = strtotime('1 day', $day);
}
    return date('F d Y', $day); 
}

$ten_days = working_days('10');

echo ($ten_days);

3 个答案:

答案 0 :(得分:1)

计算两个日期之间的工作日,包括假期和自定义工作周

答案不是那么简单 - 因此我的建议是使用一个类,你可以配置更多依赖于简单化的功能(或假设一个固定的语言环境和文化)。要在一定数量的工作日之后获得日期,您将:

  1. 需要指定您将要工作的工作日(默认为MON-FRI) - 该课程允许您单独启用或禁用每个工作日。
  2. 需要知道您需要考虑公共假期(国家和州)是否准确
  3. 功能方法

    /**
     * @param days, int
     * @param $format, string: dateformat (if format defined OTHERWISE int: timestamp) 
     * @param start, int: timestamp (mktime) default: time() //now
     * @param $wk, bit[]: flags for each workday (0=SUN, 6=SAT) 1=workday, 0=day off
     * @param $holiday, string[]: list of dates, YYYY-MM-DD, MM-DD 
     */
    function working_days($days, $format='', $start=null, $week=[0,1,1,1,1,1,0], $holiday=[])
    {
        if(is_null($start)) $start = time();
        if($days <= 0) return $start;
        if(count($week) != 7) trigger_error('workweek must contain bit-flags for 7 days');
        if(array_sum($week) == 0) trigger_error('workweek must contain at least one workday');
        $wd = date('w', $start);//0=sun, 6=sat
        $time = $start;
        while($days)
        {
            if(
            $week[$wd]
            && !in_array(date('Y-m-d', $time), $holiday)
            && !in_array(date('m-d', $time), $holiday)
            ) --$days; //decrement on workdays
            $wd = date('w', $time += 86400); //add one day in seconds
        }
        $time -= 86400;//include today
        return $format ? date($format, $time): $time;
    }
    
    //simple usage
    $ten_days = working_days(10, 'D F d Y');
    echo '<br>ten workingdays (MON-FRI) disregarding holidays: ',$ten_days;
    
    //work on saturdays
    $ten_days = working_days(10, 'D F d Y', null, [0,1,1,1,1,1,1]);
    echo '<br>ten workingdays (MON-SAT) disregarding holidays: ',$ten_days;
    
    //only work on weekends and specify some days off (e.g. tomorrow)
    $ten_days = working_days(10, 'D F d Y', null, [1,0,0,0,0,0,1], ['01-01', date('Y-m-d', time()+86400), '12-25']);
    echo '<br>ten workingdays (MON-FRI) excluding tomorrow and specifying with some holidays: ',$ten_days;
    

    OO方法

    // usage after including the WorkingDays class below
    // (you can specify holidays, vacation and customize workweeks)
    //  WorkingDays::$holiday[] = '12-25'; //add Dec 25th as day off
    //  WorkingDays::$sat = 1; //enable working on Saturday
    $date_after_workdays = WorkingDays::fromNow(10);
    echo 'after ten working days it will be ', date('D F d Y', $date_after_workdays);
    
    
    
    
    /**
     * class to calculate date after certain amount of working days.
     * Depending on location and contract "working days" is very relative.
     * By default this class is assuming standard western office hours.
     * 
     * @see http://en.wikipedia.org/wiki/Workweek_and_weekend
     * @see e.g. http://www.timeanddate.com/holidays/
     * @see e.g. http://www.officeholidays.com/
     * 
     * @usage
     *  WorkingDays::$holiday[] = '12-25'; //add Dec 25th as day off
     *  WorkingDays::$sat = 1; //enable working on Saturday
     *  $date_done = date('F d Y', WorkingDays::fromNow(10) );
     */
    class WorkingDays
    {
        //workweek definition
        public static $mon = 1;
        public static $tue = 1;
        public static $wed = 1;
        public static $thu = 1;
        public static $fri = 1;
        public static $sat = 0;
        public static $sun = 0;
    
        //use format MM-DD to define non-workingdays (holidays, vacation, etc.)
        public static $holiday = ['01-01', /* ... */];
    
        /**
         * @param $start, int: timestamp (time or mktime)
         * @param $days, int: workingdays from date
         * @return int: timestamp of date
         */
        public static function from($start, $days)
        {
            if($days <= 0) return $start;
            $wk = [self::$sun, self::$mon, self::$tue, self::$wed, self::$thu, self::$fri, self::$sat];
            if(array_sum($wk) == 0) trigger_error('workweek must contain at least one workday');
            $wd = date('w', $start);//0=sun, 6=sat
            $time = $start;
            while($days)
            {
                if($wk[$wd] && !in_array(date('m-d', $time), self::$holiday)) --$days; //decrement on workdays
                $wd = date('w', $time += 86400); //add one day in seconds
            }
            $time -= 86400;//include today
            return $time;
        }
    
        /**
         * "today" is included (if it is a workday)
         */
        public static function fromNow($days)
        {
            return self::from(time(), $days);
        }
    }
    

答案 1 :(得分:0)

您可以使用PHP的内置函数mktime来计算过去或将来的天数。

<?php

$ten_days = date("F d Y", mktime(0, 0, 0, date("m"), date("d") + 10, date("Y")));

mktime采用:

  • 小时
  • 分钟
  • 第二

然后您可以添加或减去任何元素的时间。在这里,将小时,分钟和秒设置为零并使用当前月,日和年的值。最后,我使用+ 10在当天添加了10天。

编辑:我刚刚意识到OP要求工作日。如果工作日意味着周一至周五,那么您可以在周一至周五的任何当天离开2周。对于周六和周日,您可以使用下一个即将到来的10周一至周五的日子。

<?php

if (date("w") == 0) { // SUNDAY
    $date_add = 12;
}
elseif (date("w") == 6) { // SATURDAY
    $date_add = 13;
}
else {
    $date_add = 14;
}

$ten_days = date("F d Y", mktime(0, 0, 0, date("m"), date("d") + $date_add, date("Y")));

print $ten_days;

答案 2 :(得分:0)

function working_days($days) {
    $count = 0;
    $day = time();

    while (true) {
        if(date('N', $day) < 6) $count++;       
        $day = strtotime('+ 1 days', $day);
        if($count >= $days ) break;
    }
    return date('F d Y', $day);
}

$ten_days = working_days(10);

echo ($ten_days);

试试这个,希望有所帮助