0

i use this url for time to minutes conversion but extra hour are shown where there is no hour in time.

my code is

<?php

$seconds=strtotime('00:11:10').'<br/>';
echo gmdate('H:i:s',$seconds).'<br/>';
$H = floor(($seconds / 3600));
$i = ($seconds / 60) % 60;


$s = $seconds % 60;
echo sprintf("%02d:%02d:%02d", $H, $i, $s);

?>

the output showing 389496:11:10 //it should 00:11:10 what the problem is...?? I really getting pain on this any help would be appreciated .

Community
  • 1
  • 1

2 Answers2

0

This is the snippet I use for HH:MM:SS to minutes conversion.

<?php

$str_time = "00:11:10";

$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;

$minutes = $time_seconds / 60;

echo $minutes;

The output, as you expect, is 11.166666666667.

If the time is 1 hour, 20 minutes and 10 seconds, the output is then: 80.166666666667

Community
  • 1
  • 1
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
-1

I'd venture to say because you append <br> to the $seconds, and then use it with math functions like $seconds/3600 ... etc .. remove the <br> and add it later, or remove the <br> altogether

$seconds=strtotime('00:11:10');
echo gmdate('H:i:s',$seconds).'<br/>';
$H = floor(($seconds / 3600));
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
echo sprintf("%02d:%02d:%02d", $H, $i, $s);
Silvertiger
  • 1,680
  • 2
  • 19
  • 32