PHP Check if current time is before specified time

前端 未结 9 2144
佛祖请我去吃肉
佛祖请我去吃肉 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:42

    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)

提交回复
热议问题