2010-11-04 51 views
0

我只想输出一个脚本的结果,通过CLI执行脚本时执行的时间。php日期格式的时差

做这件事,我在剧本

$start_time = time(); 

开始时设置一个变种,然后在年底

date('H:i:s',time() - $start_time); 

的问题是,即使当ellapsed时间可能是在至少一个小时或几分钟的范围内,它总是打印至少一小时过去了:

>>> echo date('H:i:s',1); 
01:00:01 
>>> echo date('H:i:s', 10); 
01:00:10 
>>> echo date('H:i:s',3599); 
01:59:59 
>>> echo date('H:i:s',3600); 
02:00:00 

shouldn它显示00:XX:YY,当不到一个小时过去了? 有什么我错过了,有没有一个错误?

感谢您的帮助!

+2

不能使用'日期()'是这样的:它与时间戳。值'1'实际上是1970年1月1日,格林威治标准时间0:00:01,根据你的时区调整。 – 2010-11-04 12:38:33

回答

2

不要使用日期()。当您有time() - $start_time时,结果以秒为单位。如果你想要它在mintues等,或使用以下函数将秒转换为小时,分钟和秒,将其相乘。

<?php /** 
* 
* @convert seconds to hours minutes and seconds 
* 
* @param int $seconds The number of seconds 
* 
* @return string 
* 
*/ 
function secondsToWords($seconds) { 
    /*** return value ***/ 
    $ret = ""; 

    /*** get the hours ***/ 
    $hours = intval(intval($seconds)/3600); 
    if($hours > 0) 
    { 
     $ret .= "$hours hours "; 
    } 
    /*** get the minutes ***/ 
    $minutes = bcmod((intval($seconds)/60),60); 
    if($hours > 0 || $minutes > 0) 
    { 
     $ret .= "$minutes minutes "; 
    } 

    /*** get the seconds ***/ 
    $seconds = bcmod(intval($seconds),60); 
    $ret .= "$seconds seconds"; 

    return $ret; 
} ?> 

用法示例:

<?php 
    /*** time since EPOCH ***/ 
    echo secondsToWords(time()); 
?> 
0

使用gmdate()而不是日期(),以显示正确的时间差和补偿服务器的时区

+0

时区不应该有所作为,如果你只是从另一个秒钟离开数秒钟。 – drew 2010-11-04 12:46:48

+0

gmdate()似乎正在返回正确的值,但在文档中,它显示该函数与date()相同,并且它还收到一个时间戳记作为参数 – msonsona 2010-11-04 12:58:20

+0

@ user495812:它有所不同,因为上面指定的值例子是绝对的(基于GMT)。 – stillstanding 2010-11-04 13:08:02

2

你可以试试这个功能:

<? 

$time = strtotime('2010-04-28 17:25:43'); 

echo 'event happened '.humanTiming($time).' ago'; 

function humanTiming ($time) 
{ 

    $time = time() - $time; // to get the time since that moment 

    $tokens = array (
     31536000 => 'year', 
     2592000 => 'month', 
     604800 => 'week', 
     86400 => 'day', 
     3600 => 'hour', 
     60 => 'minute', 
     1 => 'second' 
    ); 

    foreach ($tokens as $unit => $text) { 
     if ($time < $unit) continue; 
     $numberOfUnits = floor($time/$unit); 
     return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':''); 
    } 

} 


?> 

从这里: PHP How to find the time elapsed since a date time?

这会给你越来越差的更有效的手段。只需将strtotime('2010-04-28 17:25:43');替换为您的开始日期/时间(作为时间戳)。其中一个好处是功能可以在别处重新使用。

+0

不错,但目前不需要翻译成文本,谢谢! – msonsona 2010-11-04 13:08:10

2

试试这个:

echo "Time left:". date('H:i:s', (strtotime('2000-01-01 00:00:00') + (time()-$start_time))); 
0

此功能提供了一个 “完全” 格式化人类可读的时间串..

function humantime ($oldtime, $newtime = null, $returnarray = false) { 
     if(!$newtime) $newtime = time(); 
     $time = $newtime - $oldtime; // to get the time since that moment 
     $tokens = array (
       31536000 => 'year', 
       2592000 => 'month', 
       604800 => 'week', 
       86400 => 'day', 
       3600 => 'hour', 
       60 => 'minute', 
       1 => 'second' 
     ); 
     $htarray = array(); 
     foreach ($tokens as $unit => $text) { 
       if ($time < $unit) continue; 
       $numberOfUnits = floor($time/$unit); 
       $htarray[$text] = $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':''); 
       $time = $time - ($unit * $numberOfUnits); 
     } 
     if($returnarray) return $htarray; 
     return implode(' ', $htarray); 
    }