Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:31:43+00:00 2026-05-31T23:31:43+00:00

I have an online form that has a few fields with time data. I

  • 0

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;
    }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:31:44+00:00Added an answer on May 31, 2026 at 11:31 pm

    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;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have an online form which collects member(s) information and stores it into a
I have a contact form that has a drop-down for referral source. If Magazine
I have a form that handles 2 models, Vehiculo and Poliza. This is how
I have an application which logs into an online site using POST form being
I have developed an online survey that stores my data in a Microsoft SQL
the question's in the title really. I have an online form where, after a
I have searched online for a solution, but have yet to find one that
Let's say I have I have an online store with a shopping cart feature
I realize this is a basic question but I have searched online, been to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.