PHP:如何从微缩时间以分钟为单位回显剩余时间

时间:2013-11-30 07:20:57

标签: php microtime

以下显示以秒为单位的时间,并希望以分钟为单位,如何转换为分钟?

$query = "SELECT * FROM messages WHERE ip = '$ip' AND mtime >= NOW() - INTERVAL 5 MINUTE ORDER by mtime DESC";
$result = mysql_query($query);

if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_assoc($result);
    $diff = microtime(true) - $row['mtime']; //here the time difference between last sended message and this try
    $remaining = (5*60 - (int) $diff);
   echo $remaining;
}
else {
    ...
}

感谢。

1 个答案:

答案 0 :(得分:1)

一(1)分钟内有六十(60)秒。要将秒数转换为分钟,只需将秒数除以60(60)。

$tstart = (string) microtime(true); // casting to string shows type doesn't matter
sleep(3); // for variance
$tstop = microtime(true);
$diffSeconds = round($tstop-$tstart);
$diffMinutes = ceil($diffSeconds/60); // here is the division

echo $diffSeconds.'second'.($diffSeconds==1?'':'s')."\n"; // assuming plain text out
echo $diffMinutes.'minute'.($diffMinutes==1?'':'s')."\n";

输出:

  

秒:3.0001471042633

     

分钟:0.050002451737722