mflevie44 Posted September 28, 2013 Share Posted September 28, 2013 For example lets say I want to check whether the user input is in between 7.30 and 20.30 how can i implement in php? Sorry if this seems to be a stupid question, I'm a beginner Link to comment https://forums.phpfreaks.com/topic/282512-finding-time-range-between-two-given-times-in-php/ Share on other sites More sharing options...
Ch0cu3r Posted September 28, 2013 Share Posted September 28, 2013 explode the time on the : (colon) into $hour and $min variables. Compare $hour is between 7 and 20. Example code $time = '7:45'; list($hour, $min) = explode(':', $time); if($hour >= 7 && $hour <= 20) { // time is ok } else { // time is out of range } Link to comment https://forums.phpfreaks.com/topic/282512-finding-time-range-between-two-given-times-in-php/#findComment-1451610 Share on other sites More sharing options...
Barand Posted September 28, 2013 Share Posted September 28, 2013 7:15 would also be a correct entry with that logic $t1 = '7:30'; $t2 = '20:30'; $dt1 = DateTime::createFromFormat('G:i', $t1); $dt2 = DateTime::createFromFormat('G:i', $t2); $test = array ('7:15', '7:30', '12:00', '20:00', '20:30', '20:45'); foreach ($test as $t) { $dt = DateTime::createFromFormat('G:i', $t); $res = $dt1 <= $dt && $dt <= $dt2 ? 'OK' : 'Error'; echo "$t $res<br>"; } /* results **** 7:15 Error 7:30 OK 12:00 OK 20:00 OK 20:30 OK 20:45 Error ***************/ Link to comment https://forums.phpfreaks.com/topic/282512-finding-time-range-between-two-given-times-in-php/#findComment-1451618 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.