12
How can I convert a value in seconds to a format of 'hours:minutes:seconds'.
Example: 685 converted to 00:11:25
As I can do?
12
How can I convert a value in seconds to a format of 'hours:minutes:seconds'.
Example: 685 converted to 00:11:25
As I can do?
19
You can use the function gmdate()
:
echo gmdate("H:i:s", 685);
Refers to that answer soen
19
You can go splitting seconds to find hours and minutes:
$total = 685;
$horas = floor($total / 3600);
$minutos = floor(($total - ($horas * 3600)) / 60);
$segundos = floor($total % 60);
echo $horas . ":" . $minutos . ":" . $segundos;
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
This code returns 0:11:25
and not 00:11:25
as intended by the AP
5
Beyond calculation, already passed by Maniero:
$horas = floor($total / 3600);
$minutos = floor(($total - ($horas * 3600)) / 60);
$segundos = floor($total % 60);
It is important to remember that if one of the numbers is smaller than 10, then a format like this can come out:
1:4:31
When the expected should be:
01:04:31
For this you can use str_pad
or sprintf
(this last suggestion from Wallace), as I suggested in /a/51828/3635:
<?php
function getFullHour($input) {
$seconds = intval($input); //Converte para inteiro
$negative = $seconds < 0; //Verifica se é um valor negativo
if ($negative) {
$seconds = -$seconds; //Converte o negativo para positivo para poder fazer os calculos
}
$hours = floor($seconds / 3600);
$mins = floor(($seconds - ($hours * 3600)) / 60);
$secs = floor($seconds % 60);
$sign = $negative ? '-' : ''; //Adiciona o sinal de negativo se necessário
return $sign . sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
}
echo getFullHour(685), PHP_EOL; //00:11:25
echo getFullHour(140801), PHP_EOL; //39:06:41
echo getFullHour(100800), PHP_EOL; //28:00:00
echo getFullHour(-140801), PHP_EOL; //-39:06:41
echo getFullHour(-100800), PHP_EOL; //-28:00:00
See an example in repl.it: https://repl.it/@inphinit/Show-hours-over-24-hours-and-Negative-hours
-2
#include <stdio.h>
int main()
{
float time, hour, min, sec;
scanf("%f", &time);
hour = time / 3600;
min = (((time/3600) - (int)hour) * 3600)/60;
sec = (min - (int)min) * 60;
printf("%.0f\n%.0f\n%.0f\n", hour, min, sec);
return 0;
}
Browser other questions tagged php time
You are not signed in. Login or sign up in order to post.
I wanted to put wiki, but I’m not able to do it.
– brazilianldsjaguar
I speak the answer, and the question too.
– brazilianldsjaguar
I find the question valid and useful. I see no need to be converted to wiki
– Sergio