4
How to turn a value into seconds? For example: 01:32, in case it would be 92 seconds.
I would like to do this in php, I already searched the net but I can not find anything.
4
How to turn a value into seconds? For example: 01:32, in case it would be 92 seconds.
I would like to do this in php, I already searched the net but I can not find anything.
13
An option would be using the explode
:
$tempo = "01:32";
$segundos = explode(":",$tempo);
$total = ((int)$segundos[0] * 60) + (int)$segundos[1];
echo $total;
Another option would be using strtotime
$tempo = "00:01:32";
$total = strtotime('1970-01-01 '.$tempo .'UTC');
echo $total;
As explained by @bfavaretto, this option has a limit of 89999 seconds to make it work properly. That is, the limit value to be converted from 24 hours, 59 minutes and 59 seconds.
If you have the time parameter in the first option, you can use it like this:
$tempo = "01:01:32";
$segundos = explode(":",$tempo);
$total = ((int)$segundos[0] * 60 * 60)+ ((int)$segundos[1] * 60) + (int)$segundos[2];
echo $total;
I believe the second option is correct because the first if the value is 01:01:32
must return 61
@Leocaracciolo yes.. depending on how comes the variable he will have to use the second even. It’s even better.
Interesting: the second version works for up to 89999 seconds. Serves to represent hours of a day (and php for some strange reason understands up to 24 hours, 59 minutes and 59 seconds). But it does not serve to represent a duration of 25+ hours.
@bfavaretto would be nice to put this in the answer?
It can be. The question does not make clear what use will make (if it exceeds a day), so it is always good to contemplate all possibilities.
8
$tempo_total= "4:44";
sscanf($tempo_total, "%d:%d:%d", $horas, $minutos, $segundos);
$tempo_segundos = isset($segundos) ? $horas * 3600 + $minutos * 60 + $segundos : $horas * 60 + $minutos;
5
It is not the best solution (the best is from Daniel Gomes Da Silva), but also serves to represent a duration of 25+ hours and put into practice some knowledge in PHP
$tempo_total= "30:4:44";
if (substr_count($tempo_total, ':')==2){
$tempo_segundos = (int)$tempo_total*3600 + (int)(substr(strstr($tempo_total, ':'), 1))*60 + substr($tempo_total, -2);
}else{
$tempo_segundos = (int)$tempo_total*60 + substr($tempo_total, -2);
}
echo $tempo_segundos; //108284
1 - The conversion from string to integer depends on the format of the string, so PHP evaluates the format of the string and if it has no numerical value will be converted to 0(zero). If you have numeric value in your first position the value will be considered and if the value is not in the first position will be disregarded. example in the ideone
$tempo_total= "30:4:44";
$num = (int)$tempo_total;
var_dump ($num); // int(30)
2 - substr_count($tempo_total, ':')
counts the number of occurrences of the character :
(two points) to apply to the conditional, if equal to 2 means that the variable $tempo_total
is formed by hours, minutes and seconds and runs the if
and otherwise executes the else
3 - strstr($tempo_total, ':')
- returns part of the string $tempo_total
from the first occurrence of :
until the end, in this case returns :4:44
4 - substr(strstr($tempo_total, ':'), 1)
- returns part of the return of the above item :4:44
from heading 1, i.e. 4:44
. Converting to whole (see item 1) we have: example in the ideone
$num = (int)"4:44";
var_dump ($num); // int(4)
5 - substr($tempo_total, -2)
- a negative index, so PHP analyzes the string by counting N characters from the end
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
If you need the opposite => How to convert seconds to "Time:Minute:Second" format?
– rray