Jump to content

Validation of an clock value..


postbil.com

Recommended Posts

Hello Phpfreaks.

 

I am working at a project where the user type an start time and an end time for an event.

The user type in a textbox the clock like 18:45 (I am from Denmark so we don’t use PM/AM) the value will be saved in a variable called $start_time.

But how can I be sure that the user had entered a true value? How can I validate the value?

Hope you can help me!!

 

Link to comment
Share on other sites

You can solve this in a few ways.

 

You know that the hour part must be from 0-23 and the minute part from 0-59. Optionally with a leading zero for single digit hours.

 

Using regular expressions you could do this:

if (preg_match('#[0-2]?[0-9]:[0-5][0-9]#', $start_time)) {
// is valid
}

 

You could also split it up and check each part manually:

$parts = explode(':', $start_time);
if (count($parts) == 2 && $parts[0] >= 0 && $parts[0] <= 23 && $parts[1] >= 0 && $parts[1] <= 59) {
// is valid
}

 

If you want to know more about regular expressions we have an introductory tutorial that you might want to take a look at.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.