postbil.com Posted August 13, 2009 Share Posted August 13, 2009 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!! Quote Link to comment https://forums.phpfreaks.com/topic/170150-validation-of-an-clock-value/ Share on other sites More sharing options...
Daniel0 Posted August 13, 2009 Share Posted August 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170150-validation-of-an-clock-value/#findComment-897543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.