【问题标题】:How to convert hh:mm:ss to minutes如何将 hh:mm:ss 转换为分钟
【发布时间】:2014-09-18 11:38:14
【问题描述】:

我有一个时间列$data['Time'] (hh:mm:ss),我需要将其转换为分钟。我怎样才能做到这一点?当我这样写的时候:

$avg = ($data['Kilometers'] / $data['Time']) * 60;

我有这个错误

Warning: Division by zero in ... on line ..

【问题讨论】:

    标签: php


    【解决方案1】:

    试试这样的:

    function minutes($time){
    $time = explode(':', $time);
    return ($time[0]*60) + ($time[1]) + ($time[2]/60);
    }
    

    LIVE DEMO

    【讨论】:

      【解决方案2】:
      $time    = explode(':', $data['Time']);
      $minutes = ($time[0] * 60.0 + $time[1] * 1.0);
      $avg     = $minutes > 0 ? $data['Kilometers'] / $minutes : 'inf'; // if time stored is 0, then average is infinite.
      

      另一种将时间戳转换为分钟的方法是,

      $time    = date('i', strtotime($data['Time']));
      

      【讨论】:

      • 谢谢!如何编辑小数点后的位数?
      • 如果我这样写 $numberFormat = number_format($avs, 2, '.', '');我有而不是 - 'inf' 我有 0.00
      • 你可以这样写,if($avg != 'inf') numberFormat = number_format($avs, 2, '.', '');
      • 为什么不用秒计算?
      • @eek:在有效的时间戳中,我们可以假设秒小于 60。所以我没有添加。但是第二种方法已经解决了这个问题
      【解决方案3】:
      You can use following function 
      
      <?php
      
      function date2min ($hms) {
      
          $fromTime = strtotime($hms);
      
          $getMins = round(abs($fromTime) / 60,2);
      
          return $getMins;
      }
      
      
      $date   = date('H:i:s');  // $data['Time'] your desired time
      $myResult = date2min($date);
      
      and then use $myResult value according to your need.
      
      ?>
      

      【讨论】:

        【解决方案4】:

        这是一个简单的计算使用这个

            $time='01:02:10';
            $timesplit=explode(':',$time);
            $min=($timesplit[0]*60)+($timesplit[1])+($timesplit[2]>30?1:0);
            echo $min.' Min'  // 62 Min
        

        【讨论】:

          【解决方案5】:

          如果 hh:mm:ss 是一个字符串,你可以试试这个,它已经过测试并且可以工作:

           $hour=substr($string, 0,1);// we get the first two values from the hh:mm:ss string
           $hour=(int)$hour;
           $hourtomin=$hour*60;// after we have the hour we multiply by 60 to get the min
           $min=substr($string, 3,4);//now we do a substring 3 to 4 because we want to get only the min, and we don't want to get the : which is in position 2
           $min=(int)$min;
              
           $totalmin=$hourtomin+$min;// we just do a simple sum to calculate all the min
           echo $totalmin;
          

          【讨论】:

            【解决方案6】:

            您可以通过非常简单的方式做到这一点。

            $minutes=$item['time_diff'];
            $hours =   sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));
            return $hours;
            

            输出将 09:10

            【讨论】:

              【解决方案7】:
              function hourMinute2Minutes($strHourMinute) {
                  $from = date('Y-m-d 00:00:00');
                  $to = date('Y-m-d '.$strHourMinute.':00');
                  $diff = strtotime($to) - strtotime($from);
                  $minutes = $diff / 60;
                  return (int) $minutes;
              }
              
              echo hourMinute2Minutes('01:30'); // returns 90
              

              【讨论】:

              • 感谢您为 StackOverflow 做出贡献!虽然将解决方案显示为代码确实很有帮助,但如果您能对它的工作原理和原始问题的原因进行一些解释,将会更有帮助。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-12-29
              • 2013-07-30
              • 2021-05-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-11-23
              相关资源
              最近更新 更多