-1

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
nway
  • 29
  • 1
  • 6

2 Answers2

2

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";
Aashish gaba
  • 1,726
  • 1
  • 5
  • 14
1

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);
RahulN
  • 218
  • 1
  • 5
  • Thank you, exactly what I need! – nway Jul 24 '20 at 11:52
  • Beware that `gmdate()` is unreliable for long distances or high speeds because, being a *date* function, it assumes your hours and minutes aren't durations but times of the day, thus calculates the 24 module of the result. – Álvaro González Jul 25 '20 at 11:39