4

我对 PHP 编程比较陌生,遇到了一个有趣的问题。我尝试了多次搜索并找到了不同的解决方案,但没有一个适合我的确切问题。

我有 2 个 mysql 格式的日期戳(例如 2014-04-10 09:00:00)。我需要知道这两个时间戳之间的分钟差异,但必须排除下班时间、周末和节假日。

例如,今天的时间戳 (2014-04-11 14:00:00) 和星期一的时间戳 (2014-04-14 11:00:00) 必须显示为 390 分钟的结果(工作日是 08.30 到 18.00 )。

stackexchange 上的所有解决方案都将结果显示为数小时或数天,但我需要更高的准确性。

在此先感谢并道歉,有一些不清楚的地方。

4

3 回答 3

3

使用示例:

$from = '2013-09-06 15:45:32';
$to   = '2013-09-14 21:00:00';
echo some_func_name($from, $to);

输出 :

1 day, 22 hours, 14 minutes, 28 seconds

功能 :

function some_func_name($from, $to) {
    $workingDays = [1, 2, 3, 4, 5]; # date format = N
    $workingHours = ['from' => ['08', '00'], 'to' => ['17', '00']];

    $start = new DateTime($from);
    $end = new DateTime($to);

    $startP = clone $start;
    $startP->setTime(0, 0, 0);
    $endP = clone $end;
    $endP->setTime(23, 59, 59);
    $interval = new DateInterval('P1D');
    $periods = new DatePeriod($startP, $interval, $endP);

    $sum = [];
    foreach ($periods as $i => $period) {
        if (!in_array($period->format('N'), $workingDays)) continue;

        $startT = clone $period;
        $startT->setTime($workingHours['from'][0], $workingHours['from'][1]);
        if (!$i && $start->diff($startT)->invert) $startT = $start;

        $endT = clone $period;
        $endT->setTime($workingHours['to'][0], $workingHours['to'][1]);
        if (!$end->diff($endT)->invert) $endT = $end;

        #echo $startT->format('Y-m-d H:i') . ' - ' . $endT->format('Y-m-d H:i') . "\n"; # debug

        $diff = $startT->diff($endT);
        if ($diff->invert) continue;
        foreach ($diff as $k => $v) {
            if (!isset($sum[$k])) $sum[$k] = 0;
            $sum[$k] += $v;
        }
    }

    if (!$sum) return 'ccc, no time on job?';

    $spec = "P{$sum['y']}Y{$sum['m']}M{$sum['d']}DT{$sum['h']}H{$sum['i']}M{$sum['s']}S";
    $interval = new DateInterval($spec);
    $startS = new DateTime;
    $endS = clone $startS;
    $endS->sub($interval);
    $diff = $endS->diff($startS);

    $labels = [
        'y' => 'year',
        'm' => 'month',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    ];
    $return = [];
    foreach ($labels as $k => $v) {
        if ($diff->$k) {
            $return[] = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        }
    }

    return implode(', ', $return);
}

这个函数可以更短/更好;但那是你现在的工作;)

如果您希望排除假期,请参阅此示例:https ://stackoverflow.com/a/19221403/67332

于 2014-04-11T13:20:27.300 回答
1

我从这里得到了答案

对于 PHP >= 5.3.0,使用DatePeriod类。不幸的是,它几乎没有记录。

$start = new DateTime('6/30/2010');
$end = new DateTime('7/6/2010');
$oneday = new DateInterval("P1D");

$days = array();
$data = "7.5";

/* Iterate from $start up to $end+1 day, one day in each iteration.
   We add one day to the $end date, because the DatePeriod only iterates up to,
   not including, the end date. */
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
    $day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
    if($day_num < 6) { /* weekday */
        $days[$day->format("Y-m-d")] = $data;
    } 
}    
print_r($days);
于 2014-04-11T12:03:31.817 回答
1

我找到了这个并将其转换为分钟,

$start = new DateTime('2014-03-03 09:21:30');
$end = new DateTime('2014-03-11 17:23:15');
// otherwise the  end date is excluded (bug?)
$end->modify('+1 day');

$interval = $end->diff($start);

// total days
$days = $interval->days;
$days_inMin = ($interval->d*24*60) + ($interval->h*60) + $interval->i;

// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);

// best stored as array, so you can add more than one
$holidays = array('2014-03-07');

foreach($period as $dt) {
    $curr = $dt->format('D');

    // for the updated question
    if (in_array($dt->format('Y-m-d'), $holidays)) {
       $days--;
       $days_inMin -= (24*60);
    }

    // substract if Saturday or Sunday
    if ($curr == 'Sat' || $curr == 'Sun') {
        $days--;
        $days_inMin -= (24*60);
    }
}

echo 'Days: ' . $days; 
echo '<br>Days in Minutes: ' . $days_inMin . ' min = ' . $days_inMin/(24*60) . ' days';

编辑:

$office_hrs_min = $days_inMin - ($days * (14.5*60));
// as out of 24 only 8.5 hrs are working
于 2014-04-11T12:23:20.947 回答