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
https://forums.phpfreaks.com/topic/170150-validation-of-an-clock-value/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.