Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

576
Views
php store hours script, can't get midnight to work correctly

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 . " &#43;</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>';

?>

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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);}

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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 :

Midnight as 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
)

Midnight as 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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!