Question

I have an online form that has a few fields with time data. I store this data into the MySQL data base into a time field, which needs a format hh:mm:ss. If the user inputs the time in this format correctly, then i want to accept the data. I also want to allow users to input the time in standard US time, like 9:30 am or 11:25 pm or 10:27 am etc.

Basically I want to test if the time is in the proper database format first (hh:mm:ss), then if it is not, test if it is in the second accepted format (hh:mm am/pm), and if it is, then I will use the PHP function strtotime() to convert it into the database time format. If it is in neither of these formats, then we display an error message and die.

Does anyone know how to test if the value of a variable matches one of these time formats?

Pseudo PHP code of what I want to do:

<?php
    $value = //some time;
    if (is_database_time($value)){
        // good no problem
    }else if (is_usa_time($value)){
        $value = strtotime($value);
    }else{
        die("error incorrect time format, try again.");
    }
?>

** EDIT **

Thanks everyone for the help. I used some of the info here to make a function that works perfectly:

<?php
    function filter_time($key,$value){
        // this section handles the storage of time data
        if (preg_match('/^(0?\d|1\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)){
            //do nothing
        }else if (preg_match('/^(0?\d|1[0-2]):[0-5]\d\s(am|pm)$/i', $value)){
            $value = date( 'H:i:s', strtotime($value));
        }else{
            display_error('incorrect time format in '.$key.' field.');
        }
        return $value;
    }
?>
Was it helpful?

Solution

function verify_time_format()

function verify_time_format ($value) {

  $pattern1 = '/^(0?\d|1\d|2[0-3]):[0-5]\d:[0-5]\d$/';
  $pattern2 = '/^(0?\d|1[0-2]):[0-5]\d\s(am|pm)$/i';
  return preg_match ($pattern1, $value) || preg_match ($pattern2, $value);

}

Returns TRUE for following values:

2:03:32
02:03:32
23:59:59
15:23 AM
15:23 am
09:41 pm
9:41 PM
etc...

Update:

function filter_time ($key, $value) {

  $p1 = '/^(0?\d|1\d|2[0-3]):[0-5]\d:[0-5]\d$/';
  $p2 = '/^(0?\d|1[0-2]):[0-5]\d\s(am|pm)$/i';

  if (preg_match ($p1, $value) || preg_match ($p2, $value))
    $res = date ('H:i:s', strtotime ($value));

  else
    display_error ("incorrect time format in {$key} field.");

  return $res;

}

OTHER TIPS

You're already using the strtotime from PHP, and for the values you specified there really is no need to force a specific format.

What you would likely want to test for and ensure, is that the field validates with only digits, the colon, and am or pm as in Wh1T3h4Ck5 answer.

With that in place, your code would likely be similar to the following

<?php

  function valid_time($value) {//Wh1T3h4Ck5's function
    return preg_match('/^(0?\d|1[0-2]):[0-5]\d\s(am|pm)$/i', $value);
  }

  $value = //some time;
  if (vald_time($value)){
    $time_value = strtotime($value);
    echo $time_value;
  }else{
    die("Error incorrect time format, try again.");
  }
?>

Though a more elegant solution would to look into using Jquery/Javascript PRIOR to the form being submitted. You can test and warn the user of improper format before submitting to the PHP script. Leave the validation in the PHP script though as well, with other safeguards if needed.

You can use a regular expression to solve the problem pretty easily. For example:

<?php
  $databaseTimePattern = '/^(0[0-9])|(1[0-2]):[0-5][0-9]:[0-5][0-9]$/'; //Matches times in the form hh:mm:ss
  $usaTimePattern = '/^([1-9]|(1[0-2])):[0-5][0-9] [a|p]m$/'; //Matches times in the form hh:mm am/pm

  $value = //some time;
  if (preg_match($databaseTimePattern, $value)){
    // good no problem
  }else if (preg_match($usaTimePattern, $value)){
    $value = strtotime($value);
  }else{
    die("error incorrect time format, try again.");
  }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top