doupi4649 2013-11-29 13:19
浏览 133

PHP - 计算两个日期之间的工作时间,但不包括请求处于暂停状态的时间

Can some one help me write a function that calculates the number of working hours between two dates but want to exclude the time when the request had a status of "On Hold".

So lets say the request came in at 3PM friday and was closed at 3PM Wednesday, and working hours are from 8AM to 5PM pacific (Mon thru Friday)...Total working hours will be 27 hours...but if the request remained on hold from Monday 3PM till Tuesday 3PM...Actual work time on the request really becomes 18 hours instead of 27 hours.

I have recently started working on PHP and have been assigned this task which is very confusing to me. Please help

  • 写回答

3条回答 默认 最新

  • doupian9798 2013-11-29 14:58
    关注

    Calculate work time with an accuracy of 1 minute.

    WARNING: This function can take many seconds to load as it does a loop for every minute between the time span.

    <?php
    
    $request = array(
        'start' => '3PM Nov 29 2013',
        'end' => '3PM Dec 4 2013'
    );
    
    echo calculate_work($request);
    
    /**
     * Calculate work time by looping through every minute
     * @param  array $request start to end time
     * @return int  work time in minutes
     */
    function calculate_work($request)
    {
        $start = strtotime($request['start']);
        $end = strtotime($request['end']);
        $work_time = 0;
    
        /* Add 1 minute to the start so that we don't count 0 as a minute  */
        for ($time = $start + 60; $time <= $end; $time += 60)
        {
            // Weekends
            if (date('D', $time) == 'Sat' OR date('D', $time) == 'Sun')
                continue;
    
            // Non Working Hours
            if (date('Hi', $time) <= '0800' OR date('Hi', $time) > '1700')
                continue;
    
            // On Hold
            if ($time > strtotime('3PM Dec 2 2013') AND $time <= strtotime('3PM Dec 3 2013'))
                continue;
    
            $work_time++;
        }
    
        // Divide by 60 to turn minutes into hours
        return $work_time / 60;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)