Convert minute to second php

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:

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];
}

Example calls:

echo timeToSeconds('6:10');//370

echo timeToSeconds('1:6:14');//3974

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.

How to convert min to sec in php?

$m = $_POST["min"]; $s = $_POST["sec"]; $output = total in seconds ? thanks for the help!

How do you convert time to seconds?

Multiply the hours by 60 to convert it to minutes, i.e., 1 hr × 60 = 60 minutes . Multiply the minutes by 60 to obtain the number of seconds, i.e., 60 minutes × 60 = 3600 seconds .

How do you convert minutes to hours and seconds?

Converting between hours, minutes, and seconds using decimal time is relatively straightforward: time in seconds = time in minutes * 60 = time in hours * 3600. time in minutes = time in seconds / 60 = time in hours * 60. time in hours = time in minutes / 60 = time in seconds / 3600.

How do I convert hours to seconds in Java?

Program: Write a program to convert the hours into minutes and seconds in Java..
import java.io.*;.
import java.util.*;.
class Conversion {.
static void conversion(float hours).
float minutes, seconds;.
minutes = hours * 60;.
seconds = hours * 3600;.