jeff5656 Posted August 9, 2009 Share Posted August 9, 2009 How would the code look for this: IF the current time is between 8:00 am and 11:59 am, then Do this... Thanks! Link to comment https://forums.phpfreaks.com/topic/169459-solved-a-condition-for-between-two-times/ Share on other sites More sharing options...
bundyxc Posted August 9, 2009 Share Posted August 9, 2009 $now = time(); $min = mktime('8'); $max = mktime('11','59'); if (($now >= $min) && ($now =< $max)) { //Current time is between 8:00AM and 11:59AM } else { //Current time is NOT between 8:00AM and 11:59AM } Give this a shot. Keep in mind, I should have been asleep six hours ago, and I didn't bother to test. Link to comment https://forums.phpfreaks.com/topic/169459-solved-a-condition-for-between-two-times/#findComment-894061 Share on other sites More sharing options...
Bjom Posted August 9, 2009 Share Posted August 9, 2009 This is an alternative where you can use the 'natural' date formats when defining the limits: $timeCurr = time(); $timeStart = strtotime('8:00 am'); $timeEnd = strtotime('11:59 am'); if ($timeCurr > $timeStart && $timeCurr < $timeEnd) { //doStuff echo 'blah'; } Link to comment https://forums.phpfreaks.com/topic/169459-solved-a-condition-for-between-two-times/#findComment-894067 Share on other sites More sharing options...
jeff5656 Posted August 9, 2009 Author Share Posted August 9, 2009 That worked great, but I did have to change it to this because there was an error: from && ($now =< $max)) to && ($now <= $max)) Link to comment https://forums.phpfreaks.com/topic/169459-solved-a-condition-for-between-two-times/#findComment-894068 Share on other sites More sharing options...
jeff5656 Posted August 9, 2009 Author Share Posted August 9, 2009 Thanks Bjom. Whats the advantage and disadvantages between these two methods? Link to comment https://forums.phpfreaks.com/topic/169459-solved-a-condition-for-between-two-times/#findComment-894069 Share on other sites More sharing options...
Bjom Posted August 9, 2009 Share Posted August 9, 2009 it's simply a matter of preference. if you like to write 8:00 am take my approach if you like to write '8' take bundyxc's approach, if you don't mind, take either. Link to comment https://forums.phpfreaks.com/topic/169459-solved-a-condition-for-between-two-times/#findComment-894085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.