I have this simple store hours php script.
You add in the open hours of the store in the array.
Then you echo it out on the page.
However i have for example 10:00AM to 12:00AM.. and it breaks the logic.
Its supposed to say WE ARE OPEN... but it says we are CLOSED.
It works for every other time of the day.. but midnight something is wrong.
Please help? Probably something easy I am not seeing.
<?php
date_default_timezone_set('America/New_York');
$storeSchedule = [
'Monday' => ['10:00 AM' => '11:00 PM'],
'Tuesday' => ['10:00 AM' => '11:00 PM'],
'Wednesday' => ['10:00 AM' => '11:00 PM'],
'Thursday' => ['10:00 AM' => '11:00 PM'],
'Friday' => ['10:00 AM' => '12:00 AM'],
'Saturday' => ['10:00 AM' => '12:00 AM'],
'Sunday' => ['10:00 AM' => '11:00 PM']
];
$timestamp = time();
$status = '<span style="color:#fc2323">Closed now</span>';
$currentTime = (new DateTime())->setTimestamp($timestamp);
foreach ($storeSchedule[date('l', $timestamp)] as $startTime => $endTime) {
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime = DateTime::createFromFormat('h:i A', $endTime);
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = '<span style="color:#23fc41">OPEN</span>';
$todayIs = "owen";
break;
}
}
echo '<p style="padding-bottom:5px;">Hours: ' . $status . '</p>';
foreach($storeSchedule[date('l', $timestamp)] as $openHour => $closeHour) {
echo "<p style='padding-bottom:15px' class=\"show-store-hours\">" . date('l') . ": " . $openHour . " - " . $closeHour . " +</p>";
}
echo '<ul class="store-hours" style="margin: 0 auto; padding-bottom:25px">';
foreach ($storeSchedule as $storeDay => $hoursArray) {
foreach ($hoursArray as $startTime => $endTime) {
echo '<li>';
echo $storeDay;
echo ': <span>';
echo $startTime;
echo ' - ';
echo $endTime;
echo '</span></li>';
}
}
echo '<div class="clr"></div></ul>';
?>
How about trying to add 1 day to your endtime, if the endtime is midnight? I think you're seeing this result because the time of midnight is literally less than all the start times. For example, 10am to midnight on Friday is actually (time wise speaking) equivalent to 10am Friday to 12am Saturday.
There's another workaround too. You could post your closing times as 11:59:59pm and display those times ad midnight.
To fix your endtime you could do
if ($endtime < $starttime) {
$endtime=date_add($endtime, INTERVAL 1 DAY);}
Not sure if this is the best workaround...
But this worked for me.. thanks for the help.
if ( $endTime == "11:59 PM" ) {
$endTime = "12:00 AM";
}
I echo this on the page, after I already stored the OPEN or CLOSED value.
You could start with a H:i format string to compare the dateTime objects and then format them back to g:i A.
The difference is :
H:i$a = DateTime::createFromFormat('H:i', '24:00');
print_r($a);
DateTime Object ( [date] => 2017-04-30 00:00:00.000000 [timezone_type] => 3 [timezone] => Asia/Dubai )
g:i A$a = DateTime::createFromFormat('g:i A', '12:00 AM');
print_r($a);
DateTime Object ( [date] => 2017-04-29 00:00:00.000000 [timezone_type] => 3 [timezone] => Asia/Dubai )
So, you can do something like :
$a = DateTime::createFromFormat('H:i', '10:00');
$b = DateTime::createFromFormat('H:i', '24:00');
$c = new DateTime();
var_dump($c > $a && $c < $b);
bool(true)
And to format the dateTime object back to the "12:00 AM" format :
print($b->format('g:i A'));
12:00 AM