编程语言
首页 > 编程语言> > 计算PHP中2个日期之间的工作时间

计算PHP中2个日期之间的工作时间

作者:互联网

我有一个函数来返回两个日期之间的差异,但是我需要计算工作时间的差异,假设周一到周五(上午9点到下午5点30分):

//DATE DIFF FUNCTION
// Set timezone
date_default_timezone_set("GMT");

// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
        $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
        $time2 = strtotime($time2);
    }

    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2) {
        $ttime = $time1;
        $time1 = $time2;
        $time2 = $ttime;
    }

    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();

    // Loop thru all intervals
    foreach ($intervals as $interval) {
        // Set default diff to 0
        $diffs[$interval] = 0;
        // Create temp time from time1 and interval
        $ttime = strtotime("+1 " . $interval, $time1);
        // Loop until temp time is smaller than time2
        while ($time2 >= $ttime) {
            $time1 = $ttime;
            $diffs[$interval]++;
            // Create new temp time from time1 and interval
            $ttime = strtotime("+1 " . $interval, $time1);
        }
    }

    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value) {
        // Break if we have needed precission
        if ($count >= $precision) {
            break;
        }
        // Add value and interval 
        // if value is bigger than 0
        if ($value > 0) {
            // Add s if value is not 1
            if ($value != 1) {
                $interval .= "s";
            }
            // Add value and interval to times array
            $times[] = $value . " " . $interval;
            $count++;
        }
    }

    // Return string with times
    return implode(", ", $times);
}

日期1 = 2012-03-24 03:58:58
日期2 = 2012-03-22 11:29:16

是否有一种简单的方法可以做到这一点,即 – 计算一周内工作时间的百分比并使用上述功能划分差异 – 我已经玩弄了这个想法,得到了一些非常奇怪的数字……

还是有更好的方法……?

解决方法:

此示例使用PHP的内置DateTime类来执行日期数学运算.我如何处理这个问题的方法是先计算两个日期之间的完整工作日数,然后乘以8(见注释).然后它会在部分日期中获得工作时间并将其添加到工作总时数中.将其转换为函数将非常简单.

笔记:

>不考虑时间戳.但是你已经知道如何做到这一点.
>不处理假期. (可以通过使用一系列假期并将其添加到周六和周日过滤的位置来轻松添加.
>需要PHP 5.3.6
>假设工作日为8小时.如果员工不吃午饭,请更换$hours = $days * 8;到$小时= $天* 8.5;

.

<?php
// Initial datetimes
$date1 = new DateTime('2012-03-22 11:29:16');
$date2 = new DateTime('2012-03-24 03:58:58');

// Set first datetime to midnight of next day
$start = clone $date1;
$start->modify('+1 day');
$start->modify('midnight');

// Set second datetime to midnight of that day
$end = clone $date2;
$end->modify('midnight');

// Count the number of full days between both dates
$days = 0;

// Loop through each day between two dates
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
    // If it is a weekend don't count it
    if (!in_array($dt->format('l'), array('Saturday', 'Sunday'))) {
        $days++;
    }
}

// Assume 8 hour workdays
$hours = $days * 8;

// Get the number of hours worked on the first day
$date1->modify('5:30 PM');
$diff = $date1->diff($start);
$hours += $diff->h;

// Get the number of hours worked the second day
$date1->modify('8 AM');
$diff = $date2->diff($end);
$hours += $diff->h;

echo $hours;

See it in action

参考

> DateTime Class
> DatePeriod Class
> DateInterval Class

标签:php,date,time,date-math
来源: https://codeday.me/bug/20190530/1182530.html