Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KikoLdasd's avatar

PHP problem when comparing time

Hi, I have a weirder problem when using "date" for e.g

if (date('H') >= 23 || date('H') < 9) {
	not works
	}

If I have time longer than 23 pm and less than 9 in if it should show me something Not working But if I put more

if (date('H') >= 1 || date('H') < 9) {
		works
	}

If I put it after 00 1 AM it works I want something to be turned off between 11pm and 9am, but that's the problem when I turn 23

Do you have any idea why it doesn't work?

0 likes
2 replies
automica's avatar

using Carbon, I dont get the problem you are reporting.

function checkActive(int $hour): string
{
    if ($hour >= 23 || $hour < 9) {
        return 'inactive';
    }
    return 'active';
}

        $hours = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23];

        foreach ($hours as $hour){
            dump(checkActive($hour));
        }

outputs

"inactive"
"inactive"
"inactive"
"inactive"
"inactive"
"inactive"
"inactive"
"inactive"
"inactive"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"active"
"inactive"

i set hour using:

    $hour = \Carbon\Carbon::now()->format('H');
click's avatar

Note that date('H') returns a string and not an integer. And also note that it might be possible that date() might be in a different timezone than you expect it to be.

You should be able to easily debug this yourself by printing the results or adding it to your log.

Please or to participate in this conversation.