计算2个Datetime PHP工作小时

计算2个Datetime PHP工作小时,php,Php,我有问题 我的函数工作,但我的函数不计算分和秒 这是我的代码函数 public static function sla_response($dataopen,$dataarrival,$addslar){ $startDate = new DateTime($dataopen); $endDate = new DateTime($dataarrival); $periodInterval = new DateInterval( "PT1H" ); $period = new DatePeri

我有问题

我的函数工作,但我的函数不计算分和秒

这是我的代码函数

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_响应($dataopen、$dataarrival、$addslar){
$startDate=新日期时间($dataopen);
$endDate=新日期时间($dataarrival);
$periodInterval=新日期间隔(“PT1H”);
$period=new DatePeriod($startDate,$periodInterval,$endDate);
$count=0;
foreach($期间为$日期){
$startofday=克隆$date;
$startofday->setTime(7,00);
$endofday=克隆$date;
$endofday->setTime(17,00);
如果($date>$startofday&&$date格式('l'),数组('Sunday','Saturday')){
$count++;
}
}
//获得秒的开始时间
$start_d=日期(“Y-m-d H:00:00”,标准时间($start));
$start\u d\u seconds=strotime($start\u d);
$start\u t\u seconds=strotime($start);
$start_seconds=$start_t_seconds-$start_d_seconds;
//获得秒的结束时间
$end_d=日期(“Y-m-d H:00:00”,标准时间($end));
$end_d_seconds=strotime($end_d);
$end\u t\u seconds=strotime($end);
$end_seconds=$end_t_seconds-$end_d_seconds;
$diff=$end_seconds-$start_seconds-$addslar;
如果($diff!=0):
$count--;
endif;
$total_min_sec=日期('i:s',$diff);
返回$count.“:”$total_min_sec;}
这就是捕获结果

我要计算SLA响应开放票和到达日期时间

这个日期时间

这个结果

为了证明两个日期之间的计算有效,我为您做了以下部分:



在哪里声明了$start和$end变量?
<?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));

?>