Reputation: 384
In my application (PHP) i need to show the warning message between 23:00:00 to 05:00:00 hrs. How to write a condition for the above scenario because start time is today and end time will be tomorrow.
Thanks in advance.
Upvotes: 1
Views: 1386
Reputation: 6190
Please see the below code.
You have to specify the correct timezone. Otherwise the message will be displayed in unexpected time.
$timezone = "Asia/Calcutta";
date_default_timezone_set($timezone);
$hour = date("H");
if($hour >= 23 || $hour < 5)
{
echo "Error Message";
}
Cheers!
Prasad
Upvotes: 4
Reputation: 44376
Don't bother about date, the time is all you need:
$currentHour = (int) date('H');
if ($currentHour >= 23 || $currentHour < 5) {
// warning here
}
Upvotes: 9
Reputation: 34837
That's quite a simple check. Just see if the hour of the day is either smaller then 5 or 23 or larger, like this:
if(date('H') < 5 || date('H') >= 23) {
echo 'WARNING!';
}
Upvotes: 1