计算2个日期时间PHP工作时间

时间:2018-04-24 07:04:27

标签: php

我有问题。

我的功能工作,但我的功能无法计算分钟和秒。

这是我的代码功能。

public static function sla_response($dataopen,$dataarrival,$addslar){

$startDate = new DateTime($dataopen);
$endDate = new DateTime($dataarrival);
$periodInterval = new DateInterval( "PT1H" );

$period = new DatePeriod( $startDate, $periodInterval, $endDate );
$count = 0;

foreach($period as $date){

$startofday = clone $date;
$startofday->setTime(7,00);

$endofday = clone $date;
$endofday->setTime(17,00);

    if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Sunday','Saturday'))){

        $count++;
    }

}

//Get seconds of Start time
$start_d = date("Y-m-d H:00:00", strtotime($start));
$start_d_seconds = strtotime($start_d);
$start_t_seconds = strtotime($start);
$start_seconds = $start_t_seconds - $start_d_seconds;

//Get seconds of End time
$end_d = date("Y-m-d H:00:00", strtotime($end));
$end_d_seconds = strtotime($end_d);
$end_t_seconds = strtotime($end);
$end_seconds = $end_t_seconds - $end_d_seconds;

$diff = $end_seconds-$start_seconds-$addslar;

if($diff!=0):
    $count--;
endif;

$total_min_sec = date('i:s',$diff);

return $count .":".$total_min_sec;}

这是捕获结果。

我想计算SLA响应打开故障单和到达日期时间。

此DateTime

enter image description here

此结果

enter image description here

1 个答案:

答案 0 :(得分:0)

  

为了证明两个日期工作之间的计算,我在下面为您做了这一部分:

<?php

    /* Calculate current timestamp */
    $micro_date = microtime();
    $date_array = explode(" ",$micro_date);
    $date = date("Y-m-d H:i:s",$date_array[1]);
    $date_array[0] = round($date_array[0], 4);
    $date_array[0] = substr($date_array[0], 1, 4);
    $current_time = $date . $date_array[0];

    $start_time = "2018-04-16 06:40:21";
    echo "<BR>" . "Hardcode Start time: " . $start_time;
    echo "<BR>" . "Current time: " . $current_time = substr($current_time, 0, 19);
    echo "<BR>";

    /* Convert string to unix timestamp */
    echo "<BR>" . "Starttime: " . $time1a = strtotime($start_time);
    echo "<BR>" . "Endtime: " . $time2a = strtotime($current_time);
    echo "<BR>";

    /* Convert unix timestamp to date */
    echo "<BR>" . "Starttime: " . $time1b = date('H:i:s', ($time1a));
    echo "<BR>" . "Endtime: " . $time2b = date('H:i:s', ($time2a));
    echo "<BR>";

    /* Calculate time difference */
    echo "<BR>" . "Timediff: " . $timediff = date('H:i:s', ($time2a - $time1a));

?>

Check the result here