获取实际工作时间,排除休息时间和星期六,星期天

    PHP与Mysql admin 4530次浏览 已收录 0个评论

    一个同事写的

    <?php
    /**
    * function: 获取实际工作时间
    * author: 华仔 2011/12/19
    * params:
    *     @$start_time:string 开始时间
    *     @end_time:string 结束时间
    *     @reserveascii:boolean 如果为ascii,是否设置为优先编码
    * return:array(
    *     ‘hours’, ‘days’
    * )
    * TEST:
    *     print_r(get_actual_time_of_work(‘2011-12-19 14:00:00’, ‘2011-12-30 18:00:00’));
    */

    function get_actual_time_of_work($start_time, $end_time) {

    date_default_timezone_set(‘asia/shanghai’);

    // 定义一周工作日期 和 每天工作时间 和 午休时间
    $weekdays           = array(1, 2, 3, 4, 5);
    $work_time          = array(‘9:00′, ’18:00′);
    $lunch_break_time   = array(’12:00′, ’13:00’);

    $start_timestamp    = strtotime($start_time);
    $end_timestamp      = strtotime($end_time);

    $start_week         = date(‘W’, $start_timestamp);
    $start_weekday      = date(‘N’, $start_timestamp);
    $start_time         = date(‘H:i’, $start_timestamp);

    $end_week           = date(‘W’, $end_timestamp);
    $end_weekday        = date(‘N’, $end_timestamp);
    $end_time           = date(‘H:i’, $end_timestamp);

    $full_days          = 0;
    $full_weeks         = $end_week – $start_week – 1;
    $passed_second      = 0;

    $day_work_second    = (strtotime($work_time[1]) – strtotime($work_time[0]))
    – (strtotime($lunch_break_time[1]) – strtotime($lunch_break_time[0]));

    foreach($weekdays as $weekday) {
    if ($weekday > $start_weekday) {
    $full_days += 1;
    }

    if ($weekday < $end_weekday) {
    $full_days += 1;
    }
    }

    if ($start_timestamp < strtotime($work_time[1])) {

    $passed_second += strtotime($work_time[1]) – $start_timestamp;

    // 在午餐时间开始任务
    if ($start_timestamp > strtotime($lunch_break_time[0])
    && $start_timestamp < strtotime($lunch_break_time[1])) {

    $passed_second -= strtotime($lunch_break_time[1]) – $start_timestamp;
    }

    // 上午开始任务
    if ($start_timestamp <= strtotime($lunch_break_time[0])) {

    $passed_second -= strtotime($lunch_break_time[1]) – strtotime($lunch_break_time[0]);
    }

    // 早于上班时间开始任务
    if ($start_timestamp < strtotime($work_time[0])) {
    $passed_second -= strtotime($work_time[0]) – $start_timestamp;
    }

    }

    if ($end_timestamp > strtotime($work_time[0])) {

    $passed_second += $end_timestamp – strtotime($work_time[0]);

    // 在午餐时间结束任务
    if ($end_timestamp > strtotime($lunch_break_time[0])
    && $end_timestamp < strtotime($lunch_break_time[1])) {

    $passed_second -= $end_timestamp – strtotime($lunch_break_time[0]);
    }

    // 下午结束任务
    if ($end_timestamp >= strtotime($lunch_break_time[1])) {

    $passed_second -= strtotime($lunch_break_time[1]) – strtotime($lunch_break_time[0]);
    }

    // 加班时间完成任务
    if ($end_timestamp > strtotime($work_time[1])) {
    $passed_second -= $end_timestamp – strtotime($work_time[1]);
    }

    }

    $ret = array(
    ‘hours’ => (count($weekdays) * $full_weeks + $full_days) * $day_work_second / 3600
    + $passed_second / 3600,
    ‘days’  => count($weekdays) * $full_weeks + $full_days + $passed_second / $day_work_second
    );

    return array(
    ‘hours’ => sprintf(‘%0.1f’, $ret[‘hours’]),
    ‘days’  => sprintf(‘%0.1f’, $ret[‘days’])
    );
    }

    print_r(get_actual_time_of_work(‘2011-12-19 14:00:00’, ‘2011-12-19 19:00:00’));


    乐趣公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明获取实际工作时间,排除休息时间和星期六,星期天
    喜欢 (0)
    发表我的评论
    取消评论
    表情 贴图 加粗 删除线 居中 斜体 签到

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址