php - Determine if the store is open?

380

Solution:

In the past, I've handled this by using a time stamp without a date (seconds since midnight). So for Saturday, the open would be62100 and the close would be91800.

My thought was this removes some of the logic needed when a close crosses midnight, as you only need to compare the seconds since the start of the date to the time range.

And it's pretty easy to check if it's still open from 'yesterday' - just add86400 to the current 'time' (seconds since the start of the day) and check against the previous day.

Probably all a single SQL statement.

824

Answer

Solution:

You can use the PHP date() function and compare it to your opening hours.

You can do something like this recursive function (not working PHP code, but PHP combined with pseudo-code):

/* $current_time should be in the format of date("His") */
function check_hours($current_day, $current_time)
{
    Get the MySQL row for today here

    if (Opening_Hours.enabled == 1 WHERE Opening_Hours.week_day == $current_day)
    {
        if ((date("His") >= Opening_Hours.open_hour) and ($current_time <= Opening_Hours.close_hour))
        {
            // convert_numeric_day_to_full_representation isn't a real function! make one
            return 'Open: ' . convert_numeric_day_to_full_representation($current_day) . ' ' . Opening_Hours.open_hour . ' – ' . Opening_Hours.close_hour;
        }
        elseif (date("His") < Opening_Hours.open_hour)
        {
            return 'Closed: Next opening hours: ' . convert_numeric_day_to_full_representation($current_day) . ' ' . Opening_Hours.open_hour . ' – ' . Opening_Hours.close_hour;
        }
        else
        {
            return check_hours($tomorrow, '000000');
        }
    }
    else
    {
        return check_hours($tomorrow, '000000');
    }
}

People are also looking for solutions to the problem: PHP: How to echo remaining time in minutes from microtime

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.