0

简报:

我正在尝试计算我正在构建的自定义项目管理系统的两个日期之间的工作日。这是我到目前为止所做的,如下所示。它似乎工作正常,但我不确定它有多准确以及我是否应该使用它。任何反馈将不胜感激!

代码

<?php

    # date variables:
    date_default_timezone_set('Asia/Kuwait');
    $date['start'] = date('Y-m-d H:i:s');
    $date['end'] = '2013-12-01 08:00:00';
    $date['off'] = array('Friday','Saturday'); # usual days off in Kuwait)

    # calculate the difference:
    $date_s = new DateTime($date['start']);
    $date_e = new DateTime($date['end']);
    $interval = $date_s->diff($date_e);

    # obtain relevant values:
    $remaining_days = $interval->format('%r%a');
    $remaining_weeks = floor($remaining_days/7);
    $weekend_days = ($remaining_weeks*count($date['off']));

    # additional holidays (just an example):
    $extra_holidays_array = array
    (
        'holiday 1' => '2013-06-24 00:00:00',
        'holiday 2' => '2013-06-25 00:00:00',
        'holiday 3' => '2013-07-01 00:00:00',
        'holiday 4' => '2013-08-24 00:00:00'
    );

    # check if a real holiday:
    $extra_holidays = 0;
    foreach( $extra_holidays_array as $check_date )
    {
        $day_of_holiday = date('l', strtotime($check_date));
        if( ! in_array($day_of_holiday,$date['off']) ){ $extra_holidays++; }
    }

    # total holidays:
    $total_holidays = ($weekend_days+$extra_holidays);

    # business days:
    $business_days_nh = ($remaining_days-$weekend_days); # NO extra holidays
    $business_days_wh = ($remaining_days-$weekend_days-$extra_holidays); # WITH extra holidays

?>

<ul>
<li>Current Date: <?php echo $date['start']; ?></li>
<li>Deadline Date: <?php echo $date['end']; ?></li>
</ul>
<ul>
<li>Remaining Days: <?php echo $remaining_days; ?></li>
<li>Remaining Weeks: <?php echo $remaining_weeks; ?></li>
</ul>
<ul>
<li>Usual Holidays: <?php echo $weekend_days; ?></li>
<li>Extra Holidays: <?php echo $extra_holidays; ?></li>
<li>Total Holidays: <?php echo $total_holidays; ?></li>
</ul>
<ul>
<li>Business Days (Before Holidays): <?php echo $business_days_nh; ?></li>
<li>Business Days (After Holidays): <?php echo $business_days_wh; ?></li>
</ul>
4

1 回答 1

1

为此,我在以前的项目中编写了一个函数:

以下是用作参数的内容:

  • $start - 启动 Unix 时间戳(你可以使用 strtotime('1st january 2013') 来获取时间戳
  • $end - 结束 unix 时间戳
  • 可选的 $holidays - 一个算作假期的日期数组(例如银行假期等),它用于strtotime()转换日期
  • 可选 $returnAsArray - 返回工作日时间戳列表,或者只是工作日数

重要 重要的是要知道,给定的时间戳应该是一天开始的时间戳,例如:

要将 2013 年 1 月 1 日用作时间戳,请使用以下值:mktime(0, 0, 0, 1, 1, 2013);

function networkDays($start, $end, array $holidays = array(), $returnAsArray = false)
{
    if(!is_int($start)){ 
        trigger_error('Parameter 1 expected to be integer timestamp. ' . ucfirst(gettype($start)) . ' given.', E_USER_WARNING);
        return false;
    }

    if(!is_int($end)){ 
        trigger_error('Parameter 2 expected to be integer timestamp. ' . ucfirst(gettype($start)) . ' given.', E_USER_WARNING);
        return false;
    }

    if(!is_array($holidays)){ 
        $holidays = array();
        trigger_error('Parameter 3 expected to be Array. ' . ucfirst(gettype($holidays)) . ' given.', E_USER_NOTICE);
    }

    if(!is_bool($returnAsArray)){ 
        trigger_error('Parameter 4 expected to be Boolean. ' . ucfirt(gettype($returnAsArray)) . ' given.', E_USER_WARNING);
        return false;
    }


    if($start>=$end){ 
        $nEnd = $start;
        $nStart = $end;

        $start = $nStart;
        $end = $nEnd;
    }


    foreach($holidays as $key => $holiday)
    {
        $holidays[$key] = strtotime($holiday);
    }


    $numberOfDays = ceil(((($end-$start)/60)/60)/24);

    $networkDay = 0;
    $networkDayArray = array();

    for($d = 0; $d < $numberOfDays; $d++)
    {
        $dayTimestamp = $start+(86400*$d);

        if(date('N',$dayTimestamp)<6 && !in_array($dayTimestamp,$holidays))
        {
            $networkDay += 1;
            $networkDayArray[] = $dayTimestamp;
        }
    }


    if($returnAsArray)
    {
        return $networkDayArray;
    } else {
        return $networkDay;
    }
}
于 2013-05-27T16:56:44.113 回答