PHP Check if current time is before specified time

前端 未结 9 2047
佛祖请我去吃肉
佛祖请我去吃肉 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;
        }
    
    0 讨论(0)
  • 2020-12-29 02:52

    If you want to check whether the time is before 2.30 pm ,you can try the following code segment .

    if (date('H') < 14.30) {
       $pre2pm = true;   
    }else{
       $pre2pm = false;
    }
    
    0 讨论(0)
  • 2020-12-29 02:55

    You could just pass in the time

    if (time() < strtotime('2 pm')) {
       //not yet 2 pm
    }
    

    Or pass in the date explicitly as well

    if (time() < strtotime('2 pm ' . date('d-m-Y'))) {
       //not yet 2 pm
    }
    
    0 讨论(0)
提交回复
热议问题