TransWikia.com

PHP; time conversion form distance and speed

Stack Overflow Asked by nway on December 27, 2021

I have problem in converting time which I got from speed and distance, here’s my code;

$dist = 30; // Distance which is in kilometres(km)
// speed is in knots(kt), if I take speed of 40kt and convert it into kilometres(km/h) ... 40 * 1.852 = 74.08
$time = ($dist / 74.08) / 24;

// result is 0.016873650107991 which is correct, but my problem is how can this be format in H:m:s,
// tried with date('h:m:s', strtotime($time) but result is always 01:01:00 no mater the distance
echo $time;

Any ideas how to format the time into H:m:s or improve this code?
I have search the stack but did not found similar problem, if I missed sorry for duplication, but link is more than welcome.

2 Answers

This will also cover the case if the number of hours is > 24

$dist = 10; // Distance which is in kilometres(km)

$ts = ($dist / 74.08) * 3600; // in seconds

$h = floor($ts/3600);
$m = floor(($ts / 60) % 60);
$s = $ts % 60;

echo "$h:$m:$s";

Answered by Aashish gaba on December 27, 2021

This might help,

$dist = 10; // Distance which is in kilometres(km)
// speed is in knots(kt), if I take speed of 40kt and convert it into kilometres(km/h) ... 40 * 1.852 = 74.08
$time = ($dist / 74.08);

echo gmdate("H:i:s", $time * 3600);

Answered by RahulN on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP