Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php:无法在一年内增加两次(工作时间)_Php - Fatal编程技术网

php:无法在一年内增加两次(工作时间)

php:无法在一年内增加两次(工作时间),php,Php,我必须在一年内增加工作时间。下面给出了如何增加工作时间 $time1 = 411:20 ; $time2 = 107:15 ; strotime($time1)+strotime($time2)//不起作用 $timestamp = strtotime($time1." +".$time2); $endTime = date("H:i:s", $timestamp); echo $endTime; 如何在时间方面添加它们我尝试了各种解决方案,但都不起作用 请帮助这里有一个小的实用函数,用于计

我必须在一年内增加工作时间。下面给出了如何增加工作时间

$time1 = 411:20 ;
$time2 = 107:15 ;
strotime($time1)+strotime($time2)//不起作用

$timestamp = strtotime($time1." +".$time2);
$endTime = date("H:i:s", $timestamp);
echo $endTime;
如何在时间方面添加它们我尝试了各种解决方案,但都不起作用


请帮助

这里有一个小的实用函数,用于计算您的时间:

它以
xx:xx:xx
格式返回

<?php
function add_time(array $times) {
    $seconds = 0;
    foreach ($times as $time)
    {
        list($hour,$minute,$second) = array_pad(explode(':', $time),3,null);
        $seconds += $hour*3600;
        $seconds += $minute*60;
        $seconds += $second;
    }
    $hours    = floor($seconds/3600);
    $seconds -= $hours*3600;
    $minutes  = floor($seconds/60);
    $seconds -= $minutes*60;

    return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}


$time1 = "411:20";
$time2 = "107:10";

echo add_time(array($time1, $time2));

创建时间数组,即:

$timearray=array("411:20","107:15");
$tot=AddTime($time);
echo $tot;
AddTime函数如下所示:

public function AddTime($times) {
    $minutes=0;
// loop throught all the times
    foreach ($times as $time) {
        list($hour, $minute) = explode(':', $time);
        $minutes += $hour * 60;
        $minutes += $minute;
    }
    $hours = floor($minutes / 60);
    $minutes -= $hours * 60;
 // returns the time already formatted
    return sprintf('%02d:%02d', $hours, $minutes);
}
您的输出将是:

518:35
试试这个解决方案

  $time1 = "411:20";
    $time2 = "107:15";

    $decimalHours = decimalHours($time1,$time2);

    function decimalHours($time1,$time2)
    {
        $hms = explode(":", $time1);
        $hms2 = explode(":", $time2);
      $t1 = ($hms[0] + ($hms[1]/60));
      $t2 = ($hms2[0] + ($hms2[1]/60));
        return ($t1 + $t2) ;
    }

echo $decimalHours;

您尝试过的“各种解决方案”是什么?请编辑您的帖子以添加它们。它也可以是000:00:00格式吗?很好,但我必须在foreach循环中使用它,它给出了错误致命错误:无法重新声明添加时间()(之前声明的declareddeclare是for循环外的函数,仅在循环内使用函数_调用。请告诉我如何添加所有时间的完整总计,如559:00、574:15、508:10、554:45等等。可以将它们全部添加到
echo add_time($array_variable_此处)
如果您的所有值都在数组中,是否有可能在foreach循环之外
  $time1 = "411:20";
    $time2 = "107:15";

    $decimalHours = decimalHours($time1,$time2);

    function decimalHours($time1,$time2)
    {
        $hms = explode(":", $time1);
        $hms2 = explode(":", $time2);
      $t1 = ($hms[0] + ($hms[1]/60));
      $t2 = ($hms2[0] + ($hms2[1]/60));
        return ($t1 + $t2) ;
    }

echo $decimalHours;