Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php convert number into minutes

Tags:

php

numbers

Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.

I need it to work in this:

date("Y-m-d H:i:s", strtotime("$now - $speed mins"));

Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc

Anyone have any ideas? Never been a maths buff.

like image 266
user1022585 Avatar asked May 23 '26 23:05

user1022585


2 Answers

Just do it with second.

date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));

If you want more precision, then

date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));
like image 199
xdazz Avatar answered May 26 '26 14:05

xdazz


You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php

With sample:

$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')
like image 27
Treur Avatar answered May 26 '26 14:05

Treur