Relm
Relm

Reputation: 8291

PHP: How to echo remaining time in minutes from microtime

The following shows time in seconds, and want it in minutes, how do i convert to minutes?

$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 {
    ...
}

Thanks.

Upvotes: 1

Views: 1201

Answers (1)

zamnuts
zamnuts

Reputation: 9592

There are sixty (60) seconds in one (1) minute. To convert seconds to minutes, simply divide the amount of seconds by sixty (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";

Outputs:

in seconds: 3.0001471042633

in minutes: 0.050002451737722

Upvotes: 1

Related Questions