【问题标题】:PHP hours and minutes between two times两次之间的 PHP 小时和分钟
【发布时间】:2014-07-03 21:50:17
【问题描述】:
$time1 = "14:45";
$time2 = "15:55";
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);

$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));

echo "<b>$hours</b> hours and<b>$minutes</b> minutes</b>";

如果变量 time1 小于变量 time2,则此代码运行良好,但如果 time1 为“23:35”且 time2 为“01:40”,它将输出“-22 小时 -55 分钟”。

我该如何纠正这个问题,即使 time1 低于 time2 变量,它仍然会输出两个时间之间的小时和分钟

此代码将采用两次示例“14:45”和“16:33”并计算每次之间的小时数和分钟数,但如果“time1”低于"time2" (time1 和 time2 是变量)

http://codepad.org/QmFdN6RV

【问题讨论】:

  • 使用 ABS() 函数应用于减法
  • 这取决于 23:35 是一天,01:40 是下一天,是否是同一天。
  • 23:35 是一天,01:40 是另一天的清晨
  • @LuisSiquot 我怎么能这样做?,ABS 不只是去掉“-”吗?
  • 你必须给出日期,一天比另一天大,否则你只能得到同一天两次之间的秒数。

标签: php


【解决方案1】:

所以不知道你真正想要什么结果 - 可能有两种方式,所以我都做;)

你还必须使用地板而不是圆形。

第一个解决方案是,您根据同一天获得差异:

<?php
$time1 = "23:45";
$time2 = "01:44";
list($hours, $minutes) = explode(':', $time1);
$firstTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$secondTimestamp = mktime($hours, $minutes);

$firstTimestamp > $secondTimestamp?$seconds = $firstTimestamp - $secondTimestamp:$seconds = $secondTimestamp - $firstTimestamp;

$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));

echo "<b>$hours</b> hours and <b>$minutes</b> minutes</b>";
?>

输出将是:22 小时和 1 分钟

第二种方式是,结束时间(命名为 secondtime)是第二天 应该是这样的

<?php
$time1 = "23:45";
$time2 = "01:24";
list($hours, $minutes) = explode(':', $time1);
$firstTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$secondTimestamp = mktime($hours, $minutes);

$seconds =  $secondTimestamp - $firstTimestamp;

$seconds<0?$seconds+=24*60*60:"";

$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));


echo "<b>$hours</b> hours and <b>$minutes</b> minutes</b>";
?>

输出将是:1 小时和 39 分钟

到键盘的网址:http://codepad.org/7plRAhZJ

【讨论】:

    【解决方案2】:

    我就是这样做的,使用strtotime():

    // If dates are not specified, strtotime() will assume both are from today
    $time1 = "23:45";
    $time2 = "01:24";
    
    // Absolute value of time difference in seconds
    $diff = abs(strtotime($time1) - strtotime($time2));
    
    // Convert $diff to minutes
    $tmins = $diff/60;
    
    // Get hours
    $hours = floor($tmins/60);
    
    // Get minutes
    $mins = $tmins%60;
    

    See demo

    这假设$time1$time2 来自同一天。如果不是,则需要指定。

    $time1 = "today 23:45";
    $time2 = "tomorrow 01:24";
    // or
    $time1 = "2014-7-3 23:45";
    $time2 = "2014-7-4 01:24";
    // or some other method of specifying the day.
    

    See demo 2

    【讨论】:

    • 这两次之间的时间应该是1h39m,而不是22h21m
    • @HannesBrolinLagerstedt 必须有某种方法来澄清时间来自哪一天。查看我的更新。
    • @HannesBrolinLagerstedt 时代的顺序总是一样的吗?即$time2 是否总是大于$time1
    【解决方案3】:

    如果秒数为负数,则添加 24 小时:

    $time1 = "23:35";
    $time2 = "1:40";  
    list($hours, $minutes) = explode(':', $time1);
    $startTimestamp = mktime($hours, $minutes);
    
    list($hours, $minutes) = explode(':', $time2);
    $endTimestamp = mktime($hours, $minutes);
    
    $seconds = $endTimestamp - $startTimestamp;
    if($seconds < 0) {
        $seconds+=60*60*24; 
    }
    
    $minutes = ($seconds / 60) % 60;
    $hours = round($seconds / (60 * 60));
    
    echo "<b>$hours</b> hours and<b>$minutes</b> minutes</b>";
    

    【讨论】:

      【解决方案4】:

      你会这样做:

      $time1 = "23:40";
      $time2 = "01:40";
      list($hours, $minutes) = explode(':', $time1);
      $startTimestamp = mktime($hours, $minutes);
      
      list($hours, $minutes) = explode(':', $time2);
      $endTimestamp = mktime($hours, $minutes);
      
      $seconds = abs($endTimestamp - $startTimestamp);
      $diff = (24*60*60)-$seconds;
      if ($diff < $seconds) {
          $seconds = $diff;
      }
      
      $minutes = ($seconds / 60) % 60; 
      $hours = round($seconds / (60 * 60));
      
      echo "<b>$hours</b> hours and<b>$minutes</b> minutes</b>"
      

      请注意,这绝不会给您带来大于 12h 的差异。

      例如,06:00 和 22:00 之间的差是 8 小时

      【讨论】:

        【解决方案5】:

        你可以使用date_diff

        $date1 = new DateTime();
        $date2 = new DateTime();
        $date1->setTimestamp($startTimestamp);
        $date2->setTimestamp($endTimestamp);
        $interval = date_diff($date1, $date2);
        //or $interval = $date1->diff($date2)
        echo $interval->format('%h hours and %m minutes');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-20
          相关资源
          最近更新 更多