Convert minutes into seconds in php

Kevin Jung picture Kevin Jung · Feb 22, 2011 · Viewed 14k times · Source

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!

Answer

JohnP picture JohnP · Feb 22, 2011

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;