Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert seconds to time format? [duplicate]

Tags:

php

time

format

For some reason I convert a time format like: 03:30 to seconds 3*3600 + 30*60, now. I wanna convert it back to its first (same) format up there. How could that be?

My attempt:

3*3600 + 30*60 = 12600   12600 / 60 = 210 / 60 = 3.5, floor(3.5) = 3 = hour 

Now, what about the minutes?

Considering the value can be like 19:00 or 02:51. I think you got the picture.

And by the way, how to convert 2:0 for example to 02:00 using RegEx?

like image 375
Dewan159 Avatar asked Oct 04 '10 14:10

Dewan159


2 Answers

This might be simpler

gmdate("H:i:s", $seconds)

PHP gmdate

like image 200
CyberJunkie Avatar answered Sep 28 '22 18:09

CyberJunkie


$hours = floor($seconds / 3600); $mins = floor($seconds / 60 % 60); $secs = floor($seconds % 60); 

If you want to get time format:

$timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs); 
like image 35
ITroubs Avatar answered Sep 28 '22 18:09

ITroubs