首页 > 解决方案 > 3 位数的碳计数小时数

问题描述

我有一个应用程序可以从几条记录中计算小时数。现在我检测到如果小时数超过 2 位数的情况。

例如,我有一个$total = '102:00:00'3 位数字,它返回给我这个错误:

A two digit minute could not be found

当我尝试这样做时会发生错误:Carbon::createFromFormat('H:i:s', $total);

我该如何解决这个问题?

谢谢

标签: phplaravelphp-carbon

解决方案


I think you should not confuse a actual Date(Time) and a time interval. DateTime oriented function will not help you. Just create your own function to add two duractions. See (pseudo-)code below

function add_duration($h1,$m1,$s1,$h2,$m2,$s2)
{
  $s3 = ($s1 + $s2)%60;
  $m3 = ($m1 + $m2 + floor(($s1 + $s2)/60))%60;
  $h3 = $h1 + $h2 + floor(($m1 + $m2 + floor(($s1 + $s2)/60))/60);
  return [$h3,m3,s3];
}

推荐阅读