47

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 only, so obviously at 0.00 each day the time will reset, and the boolean will reset from false to true.

if (current_time < 2pm) {
   // do this
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Adam Moss
  • 5,582
  • 13
  • 46
  • 64

9 Answers9

97
if (date('H') < 14) {
   $pre2pm = true;
}

For more information about the date function please see the PHP manual. I have used the following time formatter:

H = 24-hour format of an hour (00 to 23)

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Tom
  • 4,257
  • 6
  • 33
  • 49
  • 1
    Good point about minutes not being required. Mind linking to docs for `'H'`? – Merlyn Morgan-Graham Dec 12 '11 at 13:56
  • http://uk.php.net/manual/en/function.date.php Lists all the different ways of formatting the date. Thinking about it could be cleaner to use 'G', as it doesn't include a leading 0 where the hour is only 1 digit, although PHP isn't fussy enough to let that effect the result of the if statement. – Tom Dec 12 '11 at 14:16
  • 2
    Why H and not G ? – Feras Jan 11 '19 at 22:37
  • @Feras - good question! As I answered over 7 years ago I’m not sure why I went for H over G. I don’t think you’d get a different outcome but using a value without a leading 0 would make more sense. – Tom Jan 12 '19 at 23:23
34

Try:

if(date("Hi") < "1400") {
}

See: http://php.net/manual/en/function.date.php

H   24-hour format of an hour with leading zeros    00 through 23
i   Minutes with leading zeros                      00 to 59
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • 1
    heh, was going crazy trying to read that with the $ in front. Didn't help that it said 'Hi' ^^ – JohnP Dec 12 '11 at 13:30
  • Should `1400` be a string? – user2924019 Mar 13 '20 at 10:46
  • 1
    @user2924019 they will result in the same answer - https://www.php.net/manual/en/language.operators.comparison.php `If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.`. Would you get better performance? I don't know, benchmark it! Is one more clear than the other? It depends on your project/organization's style guidelines and how much you prefer brevity to being explicit. My personal preference these days would be `if((int)date("Hi") < 1400)`, but I don't regularly use PHP. – Merlyn Morgan-Graham Mar 14 '20 at 23:02
17

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
}
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
JohnP
  • 49,507
  • 13
  • 108
  • 140
7

Use 24 hour time to get round the problem like so:

$time = 1400;
$current_time = (int) date('Hi');
if($current_time < $time) {
    // do stuff
}

So 2PM equates to 14:00 in 24 hour time. If we remove the colon from the time then we can evaluate it as an integer in our comparison.

For more information about the date function please see the PHP manual. I have used the following time formatters:

H = 24-hour format of an hour (00 to 23)

i = Minutes with leading zeros (00 to 59)

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • Many years later, it's interesting to note that you don't have to convert a time with colons to an integer. PHP comparison operators are smart enough to know that '13:00' is less than '14:00'! – Mike Kormendy Dec 09 '21 at 19:18
3

You haven't told us which version of PHP you're running, although, assuming it's PHP 5.2.2+ than you should be able do it like:

$now = new DateTime();
$twoPm = new DateTime();
$twoPm->setTime(14,0); // 2:00 PM

then just ask:

if ( $now < $twoPm ){ // such comparison exists in PHP >= 5.2.2
    // do this
}

otherwise, if you're using one of older version (say, 5.0) this should do the trick (and is much simplier):

$now = time();
$twoPm = mktime(14); // first argument is HOUR

if ( $now < $twoPm ){
    // do this
}
Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
2

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

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

Use time(), date() and strtotime() functions:

if(time() > strtotime(date('Y-m-d').' 14:00') {
    //...
}
Aryeh Armon
  • 2,137
  • 2
  • 22
  • 37
0

Try with

if( time() < mktime(14, 0, 0, date("n"), date("j"), date("Y")) ) {

// do this

}
Repox
  • 15,015
  • 8
  • 54
  • 79