I have a time related php question!
I have html input boxes that asks the user for minutes and seconds like so:
minutes
seconds
and the result gets posted into a php file
I need a way to calculate the minutes + seconds into seconds. So basically, if the user inputted 2min 3sec, i need the output to be 123
in the php file, i have something like this:
$m = $_POST["min"];
$s = $_POST["sec"];
$output = total in seconds ?
I am assuming I can do something like:
$n x 60 + $s
to get the total but I am having a bit of trouble
thanks for the help!
asked Feb 22, 2011 at 6:12
Kevin JungKevin Jung
2,8557 gold badges31 silver badges34 bronze badges
2
This should sort it for you.
$minutes = isset[$_POST["min"]] ? $_POST["min"] : 0;
$secs = isset[$_POST["sec"]] ? $_POST["sec"] : 0;
$totalSecs = [$minutes * 60] + $secs;
EDITED
Looking at your HTML [once you edited], you need to modify your PHP to reflect the correct names on your HTML. So use this,
$minutes = isset[$_POST["n"]] ? $_POST["n"] : 0;
$secs = isset[$_POST["s"]] ? $_POST["s"] : 0;
answered Feb 22, 2011 at 6:15
JohnPJohnP
48.9k13 gold badges108 silver badges138 bronze badges
0
What you have there should work, but here anyway, even though it's pretty much the same.
$output=$m*60+$s;
Edit: Your problem was that *
is the multiplication operator in PHP, not x
. You also typed $n
when you meant $m
.
answered Feb 22, 2011 at 6:13
icktoofayicktoofay
124k20 gold badges242 silver badges229 bronze badges
$m = $_POST["min"];
$s = $_POST["sec"];
$sec = [$m * 60] + $s;
$output = "Total " . $sec;
answered Feb 22, 2011 at 6:15
KillerFishKillerFish
4,95415 gold badges54 silver badges63 bronze badges
If your PHP contains this:
$m = $_POST["min"];
$s = $_POST["sec"];
Then the names of the inputs must match:
minutes
seconds
Then,
$totalSeconds = $m*60 + $s;
answered Feb 22, 2011 at 6:19
Patrick FisherPatrick Fisher
7,8195 gold badges34 silver badges28 bronze badges
Skip to content
Converting hours, minutes and seconds [H:i:s] string to just seconds with PHP can be done with an adaptable function that also works for just a minutes and seconds string. The time string to seconds function: Example calls: The
function works by checking if the string after being split, contains 3 elements which are hours, minutes and seconds otherwise it is just minutes and seconds. Then the correct elements get multiplied by their respective seconds, i.e an hour is 3600 seconds and a minute is 60.function timeToSeconds[string $time]: int
{
$arr = explode[':', $time];
if [count[$arr] === 3] {
return $arr[0] * 3600 + $arr[1] * 60 + $arr[2];
}
return $arr[0] * 60 + $arr[1];
}
echo timeToSeconds['6:10'];//370
echo timeToSeconds['1:6:14'];//3974