You can use the function date
to get the current time.
Just remember that this function - despite the name - returns a string. A another answer suggests to return the time in the format "hh:mm:ss" and compare with the string "00:00:00"
. This only "works" by coincidence, since comparison of strings takes into account the lexicographical order of the characters. The detail is that the condition should be ($hora >= "00:00:00") && ($hora <= "11:59:59")
(using >=
and <=
instead of >
and <
), otherwise both midnight and 11:59:59 will be ignored.
But if you think about it, to check if the time is between 00:00 and 11:59, you just need to check the value of the hour (whatever makes the minutes and seconds):
$hora = date('H');
if ($ler == 1 || ($hora >= 0 && $hora < 12)) {
// horário entre 00:00 e 11:59
}
date('H')
returns a string with the value of the time between "00" and "23" (see in documentation). However, when comparing $hora >= 0
, the PHP converts the value of $hora
to number. Of course, if you want, you can also explicitly convert the value to a number, using intval
:
$hora = intval(date('H'));
Therefore, if the time value is greater than or equal to zero and less than 12, I know the time is between 00:00 and 11:59 (it makes the value of minutes and seconds, nor need to check them).
In fact, as the documentation ensures that the value will always be between zero and 23, just test if it is less than 12.
Time zone
Just remembering that date
will take the value of the current time using the Timezone that is set in PHP. Ex:
date_default_timezone_set('Europe/London');
echo date('H'); // 18
date_default_timezone_set('Asia/Tokyo');
echo date('H'); // 03
According to the Timezone set, the value of the current time changes (in London it is now 18h, while in Tokyo it is 3am). That is, it will be considered the time that is configured on the server where PHP is running, regardless of where the user is accessing your site.
failed to post the code
– Tales Peres
Now it worked out rsrs
– RafaelMacedo