Converting minutes and seconds in seconds I have 2 input fields to input the duration of a CD song. The first input is for minutes and the second is for seconds

When submitting the form, I must insert in the db the duration either in seconds (minutes + seconds) or NULL.

Here's my validation formula:

$warning = array();

function val_duration($min, $sec, $required = false) {
    global $warning;

    if (!empty($min) || !empty($sec)) {

        if (!empty($min) &amp;&amp; (!is_numeric($min) || !ctype_digit($min) || $min &gt; 60)) {
            $warning['min'] = 'The minutes must contain a positive numeric value between 1 and 60';
        } else {
            $min_in_sec = $min * 60;
        }

        if (!empty($sec) &amp;&amp; (!is_numeric($sec) || !ctype_digit($sec) || $sec &gt; 59)) {
            $warning['sec'] = 'The seconds must contain a positive numeric value between 1 and 59';
        } else {
            $tmpsec = $sec;
        }

        $value = $min_in_sec + $tmpsec;

    } else {

        if ($required) {
            $warning['duration'] = "The duration is a required field";
        }

        $value = NULL;
    }

    return $value;
}


// HTML 
&lt;input id="min" name="min" type="text" value="..."&gt;
&lt;input id="sec" name="sec" type="text" value="..."&gt;

// validate
$min = ltrim(trim($_POST['min']), '0');
$sec = ltrim(trim($_POST['sec']), '0');

$duration_in_secs = val_duration($min, $sec, true); // duration

// mySQL
enter $duration_in_secs in db (either seconds or NULL)
I'm not familiar with PHP too much but globals are usually considered harmful. I don't know what's the best practice in PHP but passing the $warning argument by reference seems a good idea here.
When a field is not posted you get various PHP notices. I guess it's better to avoid these notices:

PHP Notice:  Undefined index: min in /tmp/x.php on line ...
PHP Notice:  Undefined index: sec in /tmp/x.php on line ...


A robust code should handle these cases.
tmpsec and min_in_sec are not initialized in every code paths, so you can get notices about these too when there is no min key in the $_POST array:

PHP Notice:  Undefined variable: tmpsec in /tmp/x.php on line 24
PHP Notice:  Undefined variable: min_in_sec in /tmp/x.php on line 2


I think it's cleaner to initialize all variables. It also could help debugging since you don't have to ignore lots of PHP notice messages when you are looking for an uninitialized global variable.
tmpsec and min_in_sec could be omitted if you reorganize the code to not count anything until it is sure that there isn't any input error. (See below.)
A small note: The function is a little bit inconsistent: it allows 60 as minute but doesn't as seconds. It would be cleaner and more common to set one hour as hours = 1, minutes = 0 and seconds = 0.




$warning = array();

function val_duration(&amp;$warning, $min, $sec, $required = false) {
    if (empty($min) &amp;&amp; empty($sec)) {
        if ($required) {
            $warning['duration'] = "The duration is a required field";
        }
        return NULL;
    }

    if (!ctype_digit($min) || $min &gt; 61) {
        $warning['min'] = 'The minutes must contain a positive numeric value between 1 and 60';
    }
    if (!ctype_digit($sec) || $sec &gt; 59) {
        $warning['sec'] = 'The seconds must contain a positive numeric value between 1 and 59';
    }
    if (!empty($warning)) {
        return NULL;
    }

    return $min * 60 + $sec;
}