【问题标题】:Diffrence between two time stamp if hour greater than 24如果小时数大于 24,则两个时间戳之间的差异
【发布时间】:2014-06-11 13:01:05
【问题描述】:

我只是编辑我的问题 我有两种时间格式我想要它们之间的区别

例如

 $time1 = new DateTime('09:00:59');
 $time2 = new DateTime('100:30:00');
 $interval = $time1->diff($time2);
   echo $interval->format('%h:%i:%s second(s)'); 
  ?>

如果我增加 time2,它在 24 小时以下工作正常,会显示致命错误

$time2 = new DateTime('100:30:00');

致命错误:未捕获的异常 'Exception' 带有消息 'DateTime::__construct() [datetime.--construct]:无法在位置 0 (1) 解析时间字符串 (100:30:00):意外字符'在 D:\xampp\htdocs\datetime.php:3 堆栈跟踪:#0 D:\xampp\htdocs\datetime.php(3): DateTime->__construct('100:30:00') #1 {main}在第 3 行的 D:\xampp\htdocs\datetime.php 中抛出

有没有其他方法或者我可以编辑相同我尝试了很多但没有找到解决方案 我只想要使用任何方法的差异 谢谢

【问题讨论】:

  • 能贴出你的相关代码吗? - 你已经标记了strtotime,但那是时间戳,所以 24 是上限,因为那是午夜 12 点所以你需要包括天数吗?
  • 你必须包括日期,你可以从一个时间戳中减去一个时间戳而无需任何转换只要你使用完整的时间戳,以便php可以根据UNIX epoc 1/1确定数值/1970.

标签: php date timestamp strtotime mktime


【解决方案1】:

一种方法:

$time2 = '100:00:00';
$time1 = '10:30:00';

list($hours, $minutes, $seconds) = explode(':', $time2);
$interval2 = $hours*3600 + $minutes*60 + $seconds;

list($hours, $minutes, $seconds) = explode(':', $time1);
$interval1 = $hours*3600 + $minutes*60 + $seconds;

$diff = $interval2 - $interval1;

echo floor($diff / 3600) . ':' . 
     str_pad(floor($diff / 60) % 60, 2, '0') . ':' . 
     str_pad($diff % 60, 2, '0');

输出:

89:30:00

这里是Codepad演示

【讨论】:

    【解决方案2】:

    我希望这会有所帮助。

    $time1 = '10:30:00';
    $time2 = '100:00:00';
    
    
    function hms2sec ($hms) {
        list($h, $m, $s) = explode (":", $hms);
        $seconds = 0;
        $seconds += (intval($h) * 3600);
        $seconds += (intval($m) * 60);
        $seconds += (intval($s));
        return $seconds;
    }
    $ts1=hms2sec($time2);
    $ts2=hms2sec($time1);
    $time_diff = $ts1-$ts2;
    
    function seconds($seconds) {
    
            // CONVERT TO HH:MM:SS
            $hours = floor($seconds/3600);
            $remainder_1 = ($seconds % 3600);
            $minutes = floor($remainder_1 / 60);
            $seconds = ($remainder_1 % 60);
    
            // PREP THE VALUES
            if(strlen($hours) == 1) {
                $hours = "0".$hours;
            }
    
            if(strlen($minutes) == 1) {
                $minutes = "0".$minutes;
            }
    
            if(strlen($seconds) == 1) {
                $seconds = "0".$seconds;
            }
    
            return $hours.":".$minutes.":".$seconds;
    
            }
    echo $final_diff=seconds($time_diff);
    

    【讨论】:

      【解决方案3】:

      因为我没有足够的“声誉”来为所选答案添加评论。我想添加一条消息来指出它的缺陷。

      如果您尝试使用这些参数:

      $time2 = '10:15:00';
      $time1 = '10:10:00';
      

      你会得到一个错误的结果: 0:50:00

      要更正此问题,您需要在处理分钟的 str_pad 中添加 STR_PAD_LEFT,如下所示:

      str_pad(floor($diff / 60) % 60, 2, '0', STR_PAD_LEFT) . ':' .
      

      【讨论】:

        猜你喜欢
        • 2013-03-22
        • 2017-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        相关资源
        最近更新 更多