Aleirita Posted October 1, 2009 Share Posted October 1, 2009 as the subject says.. I need to validate TIME input in a form by the user (military hour)... I use checkdate() for the date thats input but I cant find a solution to validate the time.. "24:15" --> RIGHT!!! "32:58" --> WRONG!!! Please help ! Ale. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/ Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 Last time I checked 24:15 wasn't a valid military time where I'm from.... Assuming that was a little mistake, how about this? <?php if(strtotime($str)) { echo "Pass"; } else { echo "Fail"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928391 Share on other sites More sharing options...
Aleirita Posted October 1, 2009 Author Share Posted October 1, 2009 oooppssss yes my bad i meant 14:15 .. but anyway what you gave me didnt work either.... it validates 25:10 and 12:68 as ok! .... Ale. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928645 Share on other sites More sharing options...
mikesta707 Posted October 1, 2009 Share Posted October 1, 2009 function validateMilTime($time){ $bits = explode(':', $time); if ($bits[0] > 24 || ($bits[0] == 24 && $bits[1] > 0) || count($bits) > 2){ return false; } return true; } try that Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928650 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 oooppssss yes my bad i meant 14:15 .. but anyway what you gave me didnt work either.... it validates 25:10 and 12:68 as ok! .... Ale. Erm, I beg to differ... CODE: <?php function check_time($time) { if(strtotime($str)) { return "PASS<br/>"; } else { return "FAIL<br/>"; } } echo check_time("25:10"); echo check_time("12:68"); echo check_time("24:14"); echo check_time("23:14"); echo check_time("00:14"); ?> OUTPUT: FAIL FAIL FAIL PASS PASS Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928656 Share on other sites More sharing options...
Alex Posted October 1, 2009 Share Posted October 1, 2009 oooppssss yes my bad i meant 14:15 .. but anyway what you gave me didnt work either.... it validates 25:10 and 12:68 as ok! .... Ale. Erm, I beg to differ... CODE: <?php function check_time($time) { if(strtotime($str)) { return "PASS<br/>"; } else { return "FAIL<br/>"; } } echo check_time("25:10"); echo check_time("12:68"); echo check_time("24:14"); echo check_time("23:14"); echo check_time("00:14"); ?> OUTPUT: FAIL FAIL FAIL PASS PASS That wouldn't output that. It wouldn't even work because $str isn't defined within the scope of that function. I know you probably just put that together and perhaps made up the results because you know it's right or something, but I'd advise against that; no one is perfect and you could have made a mistake and you'll just end up causing confusion. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928660 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 Actually I tested it, I just changed it to $time afterwards so it made more sense, guess it helps if you change it in both places. <?php function check_time($time) { if(strtotime($time)) { return "PASS<br/>"; } else { return "FAIL<br/>"; } } echo check_time("25:10"); echo check_time("12:68"); echo check_time("24:14"); echo check_time("23:14"); echo check_time("00:14"); ?> Better? Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928665 Share on other sites More sharing options...
Psycho Posted October 1, 2009 Share Posted October 1, 2009 strtotime() still seems to have some problems based upon the OPs original request. 1. It accepts seconds as well, in fact it found 23:59:60 as valid 2. It accepts 00:00 as valid, but not 24:00 (which is what most people I know use for midnight) 3. It accepts negaive times I think I could craft a regular expression for this, but would need very specific criteria. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928666 Share on other sites More sharing options...
mikesta707 Posted October 1, 2009 Share Posted October 1, 2009 besides the incorrect variable, that function is perfectly fine. I just tested it function check_time($time) { if(strtotime($time)) { return "PASS<br/>"; } else { return "FAIL<br/>"; } } echo check_time("25:10"); echo check_time("12:68"); echo check_time("24:14"); echo check_time("23:14"); echo check_time("00:14"); results: FAIL FAIL FAIL PASS PASS However, if you just want to test military time, that function wont work. for example this: echo check_time("monday"); will output PASS Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928667 Share on other sites More sharing options...
Alex Posted October 1, 2009 Share Posted October 1, 2009 Actually I tested it, I just changed it to $time afterwards so it made more sense, guess it helps if you change it in both places. <?php function check_time($time) { if(strtotime($time)) { return "PASS<br/>"; } else { return "FAIL<br/>"; } } echo check_time("25:10"); echo check_time("12:68"); echo check_time("24:14"); echo check_time("23:14"); echo check_time("00:14"); ?> Better? Since it works I'd say so You don't really need a function for that though. All your function is accomplishing is returning the result of another function or more less, pretty redundant. If you're looking to echo it like your function: echo (strtotime($time)) ? 'Pass' : 'Fail'; Or more likely to be used, in a conditional: if(strtotime($time)) { } Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928670 Share on other sites More sharing options...
mikesta707 Posted October 1, 2009 Share Posted October 1, 2009 No offense mikesta707, but that is a bit incomplete. What if the value passed is "12:12:bvdfuybveyu"? Plus, if one of the first two values is not a number it may pass as well in some scenarios. I think this can be achieved with a regex pattern. But, one question, does the hour have to be two digits or can it be one? E.g. 04:30 vs. 4:30? "12:12:bvdfuybveyu" would be false, because of the count check I did. You are correct however, I did not test for string values. Here is an editted version. checks for a lot more function validateMilTime($time){ $bits = explode(':', $time); if ($bits[0] > 24 ||$bits[1] > 59|| ($bits[0] == 24 && $bits[1] > 0) || count($bits) != 2 || !is_numeric($bits[0]) || !is_numeric($bits[1])){ return "false"; } return "true"; } echo validatemilTime("25:15") . "25:15<br />"; echo validatemilTime("24:15") . "24:15<br />"; echo validatemilTime("23:15") . "25:15<br />"; echo validatemilTime("22:15:00") . "22:15:00<br />"; echo validatemilTime("ga:ad") . "ad:ad<br />"; echo validatemilTime("12:30AM") . "12:30AM<br />"; echo validatemilTime("aweae:234") . "aweaw:234<br />"; echo validatemilTime("12:60") . "12:60<br />"; output: false25:15 false24:15 true25:15 false22:15:00 falsead:ad false12:30AM falseaweaw:234 false12:60 Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928674 Share on other sites More sharing options...
Aleirita Posted October 1, 2009 Author Share Posted October 1, 2009 function validateMilTime($time){ $bits = explode(':', $time); if ($bits[0] > 24 || ($bits[0] == 24 && $bits[1] > 0) || count($bits) > 2){ return false; } return true; } Thanks that DID work! I just added the mins > 59 .. function validTime($time) { $bits = explode(':', $time); if (($bits[0] > 24) || ($bits[1] > 59) || ($bits[0] == 24 && $bits[1] > 0) || count($bits) > 2) { echo " $time is invalid!"; return 0; } else { echo " $time is valid!"; return 1; } } THANKS!!! Ale. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928676 Share on other sites More sharing options...
mikesta707 Posted October 1, 2009 Share Posted October 1, 2009 function validateMilTime($time){ $bits = explode(':', $time); if ($bits[0] > 24 || ($bits[0] == 24 && $bits[1] > 0) || count($bits) > 2){ return false; } return true; } Thanks that DID work! I just added the mins > 59 .. function validTime($time) { $bits = explode(':', $time); if (($bits[0] > 24) || ($bits[1] > 59) || ($bits[0] == 24 && $bits[1] > 0) || count($bits) > 2) { echo " $time is invalid!"; return 0; } else { echo " $time is valid!"; return 1; } } THANKS!!! Ale. ahh ok, check out my updated one, checks to make sure there are no string values too, and makes sure that it is the format hour:min Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928678 Share on other sites More sharing options...
Aleirita Posted October 1, 2009 Author Share Posted October 1, 2009 ahh ok, check out my updated one, checks to make sure there are no string values too, and makes sure that it is the format hour:min PERFECT!! thanks Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928681 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 You don't really need a function for that though. All your function is accomplishing is returning the result of another function or more less, pretty redundant. If you're looking to echo it like your function: echo (strtotime($time)) ? 'Pass' : 'Fail'; Or more likely to be used, in a conditional: if(strtotime($time)) { } Totally agreed, hence the fact thats basically what I orginally posted, but since the OP claimed it didn't work, I thought I'd prove it did. However, if you just want to test military time, that function wont work. for example this: echo check_time("monday"); Very true, I assumed (perhaps a bad thing to do) that the ability to input "monday" was unlikely. My function also allows 4:14, 1243 etc, which yours doesn't, technically I guess thats a 'bug' since it's not perfect military formatting, but they are valid times. My original thought was to use a simple regex, but since the accepable values of the second char are dependent on the first, I figured it's beyond my limited Regex ability to write. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928683 Share on other sites More sharing options...
mikesta707 Posted October 1, 2009 Share Posted October 1, 2009 As a side note, mine would allow single digit hours, like 4:14 Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928689 Share on other sites More sharing options...
Aleirita Posted October 1, 2009 Author Share Posted October 1, 2009 so... for those of you out there looking for the validating code for time .. HERE IT IS!! Ale. function validateMilTime($time){ $bits = explode(':', $time); if ($bits[0] > 24 ||$bits[1] > 59|| ($bits[0] == 24 && $bits[1] > 0) || count($bits) != 2 || !is_numeric($bits[0]) || !is_numeric($bits[1])){ return "false"; } return "true"; } echo validatemilTime("25:15") . "25:15<br />"; echo validatemilTime("24:15") . "24:15<br />"; echo validatemilTime("23:15") . "25:15<br />"; echo validatemilTime("22:15:00") . "22:15:00<br />"; echo validatemilTime("ga:ad") . "ad:ad<br />"; echo validatemilTime("12:30AM") . "12:30AM<br />"; echo validatemilTime("aweae:234") . "aweaw:234<br />"; echo validatemilTime("12:60") . "12:60<br />"; output: false25:15 false24:15 true25:15 false22:15:00 falsead:ad false12:30AM falseaweaw:234 false12:60 Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928693 Share on other sites More sharing options...
Psycho Posted October 1, 2009 Share Posted October 1, 2009 I was going to try and do reg ex as well, but decided I didn't want to invest the time. But I did use reg ex to validate the initial pattern. This function will validate that the input is two sets of two digits separated by a colon (and no other content). It then validates that the two groups of digits make a valid time. I made it to allow 00:00 and 24:00. Note: You can change it to allow a single digit for the hour by changing the first "{2}" to "{1,2}" function valid_time($time) { //Verify there are two sets of two numbers separated by colon if (!preg_match("/^\d{2}:\d{2}$/", $time)) { return false; } //Return true/false based upon hour/minute values list($hours, $minutes) = explode(':', $time); return ($hours<24 && $minutes<60 || ($hours==24 && $minutes==0)); } Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928704 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 A full regex solution is most likely going to give you the best solution without having an awefull lot of individual tests. As good as mikesta707's version is, there are ways to screw it up if thats what your trying to do. Inspiration dawned on me of a simple way to code the regex, gives a long regex, but keeps it simple, I'm not sure if it's perfect, but seems to solve everything correctly... CODE: <?php $times = array("00:00", "1:10", "31:10", "25:10", "24:00", "23:60", "23:28", "2:1", "4:10", "-12:00", "4:-3", ".2:00", "12:.343434"); function validateMilTime($time){ $bits = explode(':', $time); if ($bits[0] > 24 ||$bits[1] > 59|| ($bits[0] == 24 && $bits[1] > 0) || count($bits) != 2 || !is_numeric($bits[0]) || !is_numeric($bits[1])){ return "false"; } return "true"; } function check_time($time) { if(preg_match("/^(00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23):[0-5][0-9]$/", $time)) { return "true"; } else { return "false"; } } echo "validateMilTime<br/>"; foreach($times as $t) { echo $t . " = " . validateMilTime($t) . '<br/>'; } echo "check_time<br/>"; foreach($times as $t) { echo $t . " = " . check_time($t) . '<br/>'; } ?> OUTPUT: validateMilTime 00:00 = true 1:10 = true 31:10 = false 25:10 = false 24:00 = true 23:60 = false 23:28 = true 2:1 = true 4:10 = true -12:00 = true 4:-3 = true .2:00 = true 12:.343434 = true check_time 00:00 = true 1:10 = false 31:10 = false 25:10 = false 24:00 = false 23:60 = false 23:28 = true 2:1 = false 4:10 = false -12:00 = false 4:-3 = false .2:00 = false 12:.343434 = false Don't think I changed any variable names this time :-\ Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928718 Share on other sites More sharing options...
Psycho Posted October 1, 2009 Share Posted October 1, 2009 A more compact regex function valid_time($time) { return (preg_match("/^((([01][0-9])|([2][0-3]))[0-5][0-9]))|(24:00)$/", $time)); } Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928724 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 Damn you mjdamato. I tried an expression very similar to that but couldn't get it working. Doesn't help I've only just started learning Regular Expressions, I must have missed out a set of bracket or something. Incidently I'd personally miss the end of the expression off, I don't consider 24:00 a valid military time, midnight should be 00:00 as far as I understand it. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928728 Share on other sites More sharing options...
Psycho Posted October 2, 2009 Share Posted October 2, 2009 2400 was the way we always referred to midnight when I was in the military. Wikipedia seems to suggest that 24:00 represents the end of the day and 0:00 represents the beginning of the day. Makse sense if you are talking about work schedules. For eaxmple: 00:00 to 08:00 versus 24:00 to 08:00 or 16:00 to 24:00 versus 16:00 to 00:00 http://upload.wikimedia.org/wikipedia/en/4/44/Railtime.png[/img] Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928811 Share on other sites More sharing options...
cags Posted October 2, 2009 Share Posted October 2, 2009 Point taken, I never really thought of it like that. I was thinking of it from the approach of a digital watch or PC. At the end of the day I suppose it's up to the person using the function as to which values they wish to accept. Obviously they'd have to take into account if doing any calculations that 00:00 and 24:00 are the same value which would complicate things slightly. Based on your justifcation I think if I was personally using the function I'd accept 24:00 as a valid input but convert it to 00:00 before doing anything with it. As I say though, thats just my opinion, it's really up to personal preference. Quote Link to comment https://forums.phpfreaks.com/topic/176168-solved-validate-user-input-time/#findComment-928955 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.