0

How may I turn days from the gmdate(); into hours?

gmdate("d H:i:s", '115932'); // 02 08:12:12

What I want to return is 56:12:12

gmdate("H:i:s", '115932'); // 08:12:12

EDIT: To clarify, I just want to convert a numeric string into the H:i:s format.

ditto
  • 5,917
  • 10
  • 51
  • 88

2 Answers2

2

Extract the hours, minutes and seconds, like this:

$time = 115932;
$mins = $time % 3600;
$secs = $mins % 60;
$hours = ($time - $mins) / 3600;
$mins = ($mins - $secs) / 60;
echo $hours . ':' . (($mins < 10 ) ? '0' : '') . $mins . ':'  . (($secs < 10 ) ? '0' : '') . $secs;
// Or like this
vprintf('%02d:%02d:%02d',$hours,$mins,$secs);
Ties
  • 5,726
  • 3
  • 28
  • 37
2

To convert days d to hours, just multiply by by 24. Then add hours H.

Try this,

$date = '115932';

$minsec = gmdate("i:s", $date); 
$hours = gmdate("d", $date)*24 + gmdate("H", $date);

$time = $hours . ':' . $minsec; // 56:12:12

See demo

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • This will not work if the daylight saving time changes in that interval. Meaning it is bogus during two months per year. – hek2mgl Aug 03 '14 at 08:14
  • @hek2mgl I believe you, and will delete my answer if it's wrong, but can you explain a little more? My answer simply converts days to hours. How would daylight savings cause a bug? – Mark Miller Aug 03 '14 at 08:20
  • the number of seconds doesn't match the end time... try `$date = 100` you'll get 24:01:40 because as @hek2mgl was trying to say gmdate is for parsing dates not intervals... So I'm not sure why this is the correct answer. Plus it doesn't work that well with `$date > 31 days` – Ties Aug 03 '14 at 10:40
  • with duration = 3000 seconds you output will be: 24:50:00 – user2988257 Sep 29 '20 at 10:45