dsnpjz6907 2019-04-29 12:47
浏览 48
已采纳

如何计算负/正时间且大于24:00?

I have to make a calculation with times like these that come from an array:

+04:43
03:33
-10:33

I tried using Datetime and stuff but the class just broke when it surpassed 24:00 time (10:00 + 20:00 for example). So I tried something like this:

I transformed all my hh:mm to seconds with explode:

foreach($stringSaldo as $saldo) {
          $horaM[] =  explode(':',$saldo);
      }
      $totalHora = 0;
      $totalMin = 0;
      foreach($horaM as $hora) {
          $totalHora = ($totalHora + $hora[0]);
          $totalMin =( $totalMin + $hora[1]);
      }
      $totalHora = $totalHora * 3600;
      $totalMin = $totalMin * 60;
      $totalSeconds = $totalHora + $totalMin;

Then I tried to make that seconds in time:

$hours = floor($totalSeconds / 3600);
      $minutes = floor(($totalSeconds / 60) % 60);
      $seconds = $totalSeconds % 60;
 echo $hours. ":" . $minutes;

For some reason when I have times like: -03:34 and +01:00 the calculation fails, it gives -02:-26 but it should be -02:34.

What am I doing wrong?

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 写c++代码,第7题说一下使用的计算方法
      • ¥15 postman接口自动化测试报告实践总结
      • ¥15 有关c++的问题,利用相关知识
      • ¥15 求香农编码和解码的matlab代码
      • ¥20 ROS中的TEB局部规划问题
      • ¥20 关于#matlab#的问题:要求测出上面图片中所有同心圆的半径
      • ¥20 epanet软件运行问题
      • ¥15 Python 文件读取
      • ¥60 dpabi进行Alff计算时脑池有干扰信号
      • ¥15 利用kmeans或kshape聚类分析对归一化的无量纲时间-降雨序列进行聚类