PHP Check if current time is before specified time

前端 未结 9 2215
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 01:51

I need to check in PHP if the current time is before 2pm that day.

I\'ve done this with strtotime on dates before, however this time it\'s with a time o

9条回答
  •  青春惊慌失措
    2020-12-29 02:47

    This function will check if it's between hours in EST by accepting 2 params, arrays with the hour and am/pm...

        /**
         * Check if between hours array(12,'pm'), array(2,'pm')
         */
        function is_between_hours($h1 = array(), $h2 = array())
        {
            date_default_timezone_set('US/Eastern');
            $est_hour = date('H');
    
            $h1 = ($h1[1] == 'am') ? $h1[0] : $h1[0]+12;
            $h1 = ($h1 === 24) ? 12 : $h1;
    
            $h2 = ($h2[1] == 'am') ? $h2[0] : $h2[0]+12;
            $h2 = ($h2 === 24) ? 12 : $h2;
    
            if ( $est_hour >= $h1 && $est_hour <= ($h2-1) )
                return true;
    
            return false;
        }
    

提交回复
热议问题