Convert minutes into seconds in php

17.6k views Asked by At

I have a time related php question!

I have html input boxes that asks the user for minutes and seconds like so:

<input type="text" name="n" size="2">minutes
<input type="text" name="s" size="2">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!

4

There are 4 answers

0
JohnP On BEST ANSWER

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;
0
icktoofay On

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.

0
KillerFish On
$m = $_POST["min"]; 
$s = $_POST["sec"]; 

$sec = ($m * 60) + $s;

$output = "Total " . $sec;
0
Patrick Fisher On

If your PHP contains this:

$m = $_POST["min"]; 
$s = $_POST["sec"];

Then the names of the inputs must match:

<input type="text" name="min" size="2">minutes
<input type="text" name="sec" size="2">seconds

Then,

$totalSeconds = $m*60 + $s;