Gmdate()

Currently using gmdate to convert seconds to a formatted time to be printed out:
$sec = 144
gmdate(H:i:s, $sec);

The problem is my $sec variable can be up to 1080000 seconds which is 300 hours and my current code doesnt display this as “H” only goes up to 23. So how else would I display it? I want it to be purely in hours:mins:seconds format no days.

You’re using this wrong. gmdate(), just like date(), gives you a formatted representation of one point in time, a “date”, that’s based on the seconds elapsed since about 1970, what is called the unix timestamp. So 1080000 is not 300 hours - what may come close - but actually Tue, 13 Jan 1970 12:00:00. So you will never get an hour value of more than 23, and the calculation of gmdate implies way more special cases then just calculating some hours. If what you have is only an amount of seconds, you can just use division - you know like, a minute has 60 seconds, an hour has 60 minutes.

how would I display that in 00:00:00 form tho

use division to get hours, minutes and seconds from the amount of total second, the modulus[1] operator may help you, then use string concatenation[2].

[1] https://www.php.net/manual/en/language.operators.arithmetic.php
[2] https://www.php.net/manual/en/language.operators.string.php

Sponsor our Newsletter | Privacy Policy | Terms of Service