2014-07-10 57 views
1

我需要将时间28:10:10(在HH:MM:SS中)转换为1:04:10:10 (日:HH:MM:SS)。我想使用apply strtime()函数将输入时间转换为24小时时间格式。如何将HH:MM:SS时间转换为天数:HH:MM:SS in php

此外,我需要从它减去给定的时间。

+0

有你使用DateTime对象这个考虑? – gries

+0

关于PHP日期/时间函数的文章:** [使用PHP处理日期时间](http://www.devshed.com/c/a/PHP/DateTime-Processing-with-PHP/)** –

回答

2
**I have tried to answer your question. below is the code that will give you correct output. pls have a look on the below code.** 

function secondsToTime($ss) { 
     $s = $ss%60; 
     $m = floor(($ss%3600)/60); 
     $h = floor(($ss%86400)/3600); 
     $d = floor(($ss%2592000)/86400); 

     // Ensure all values are 2 digits, prepending zero if necessary. 
     $s = $s < 10 ? '0' . $s : $s; 
     $m = $m < 10 ? '0' . $m : $m; 
     $h = $h < 10 ? '0' . $h : $h; 
     $d = $d < 10 ? '0' . $d : $d; 

     return "$d:$h:$m:$s"; 
    } 



    $time = "28:10:10"; 
    $timeArr = array_reverse(explode(":", $time)); 
    $seconds = 0; 
    foreach ($timeArr as $key => $value) 
    { 
     if ($key > 2) break; 
     $seconds += pow(60, $key) * $value; 
    } 
    $seconds; 

    print secondsToTime($seconds); 

OUTPUT 1:04:10:10

+0

为什么要打扰几小时/分钟/秒?他们不会改变。 –

+0

为什么他们不会改变?如问题HH:MM:SS改变。 –

相关问题