supermoney
supermoney

Reputation: 79

Verify if time is between midnight and 1am in PHP

I've coded the following line for verifying if time is between midnight (00:00) and 1am clock (01:00). Is it correct?

$current_time = strtotime('now');
if ($current_time > strtotime('12:00pm') && $current_time < strtotime('1:00am')) { DO SOMETHING }

I'm not sure of that 12:00pm... Thanks in advance!

Upvotes: 0

Views: 3893

Answers (2)

MDChaara
MDChaara

Reputation: 318

Consider this:

<?php

$current_time   =   date('d M Y H:i:s');
$current_hour   =   date('H', strtotime($current_time));

if($current_hour < 1){

    //do something
}

else{

    //do something else
    }

?>

'H' will return time in hours only format. As long as the time is between 12:00 am and 1:00 am the condition will return TRUE.

Upvotes: 2

Edu C.
Edu C.

Reputation: 408

You're trying to verify between midday (12:00pm) and 1 am.. 12:00am is midnight. So you should change it to

if ($current_time > strtotime('12:00am') && $current_time < strtotime('01:00am')) { DO SOMETHING }

Upvotes: 6

Related Questions