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!
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;
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
.
$m = $_POST["min"];
$s = $_POST["sec"];
$sec = ($m * 60) + $s;
$output = "Total " . $sec;
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.