Determine If Business Is Open/Closed Based on Business Hours

Determine If Business Is Open/Closed Based On Business Hours

You would first need to create an array which will hold your days of the week, and their respective close/open time range(s).

/**
* I setup the hours for each day if they carry-over)
* everyday is open from 09:00 AM - 12:00 AM
* Sun/Sat open extra from 12:00 AM - 01:00 AM
*/
$storeSchedule = [
'Sun' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM'],
'Mon' => ['09:00 AM' => '12:00 AM'],
'Tue' => ['09:00 AM' => '12:00 AM'],
'Wed' => ['09:00 AM' => '12:00 AM'],
'Thu' => ['09:00 AM' => '12:00 AM'],
'Fri' => ['09:00 AM' => '12:00 AM'],
'Sat' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM']
];

You then loop over the current day's time range(s) and check to see if the current time or supplied timestamp is within a range. You do this by using the DateTime class to generate a DateTime object for each time range's start/end time.

The below will do this and allow you to specify a timestamp in case you are wanting to check a supplied timestamp instead of the current time.

// current or user supplied UNIX timestamp
$timestamp = time();

// default status
$status = 'closed';

// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);

// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {

// create time objects from start/end times
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime = DateTime::createFromFormat('h:i A', $endTime);

// check if current time is within a range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = 'open';
break;
}
}

echo "We are currently: $status";

See DEMO of above

Efficiently determining if a business is open or not based on store hours

If you are willing to just look at single week at a time, you can canonicalize all opening/closing times to be set numbers of minutes since the start of the week, say Sunday 0 hrs. For each store, you create a number of tuples of the form [startTime, endTime, storeId]. (For hours that spanned Sunday midnight, you'd have to create two tuples, one going to the end of the week, one starting at the beginning of the week). This set of tuples would be indexed (say, with a tree you would pre-process) on both startTime and endTime. The tuples shouldn't be that large: there are only ~10k minutes in a week, which can fit in 2 bytes. This structure would be graceful inside a MySQL table with appropriate indexes, and would be very resilient to constant insertions & deletions of records as information changed. Your query would simply be "select storeId where startTime <= time and endtime >= time", where time was the canonicalized minutes since midnight on sunday.

If information doesn't change very often, and you want to have lookups be very fast, you could solve every possible query up front and cache the results. For instance, there are only 672 quarter-hour periods in a week. With a list of businesses, each of which had a list of opening & closing times like Brandon Rhodes's solution, you could simply, iterate through every 15-minute period in a week, figure out who's open, then store the answer in a lookup table or in-memory list.

How to tell if a business is open after midnight

You can consider that closeTime must be superior to openTime, otherwise its the day after.

so it will become something like:

let realCloseTime = closeTime < openTime ? closeTime + 24 : closeTime
if now.hour! > Int(openTime) && now.hour! < Int(realCloseTime) {}

How to determine if a business is open given the hours of operation (Swift-iOS)

It's just a matter of playing with the timezone, whether you use the user system's timezone or let them choose another one in the app's settings:

let tz = NSTimeZone.defaultTimeZone()
let now = NSCalendar.currentCalendar().componentsInTimeZone(tz, fromDate: NSDate())

if now.weekDay == 2 && now.hour > MonOpen && now.hour < MonClose {
// The store is open
}

How to show Open/Closed based on time records with an html class

I've modified your code a little, making use of date.js:

http://codepen.io/anon/pen/VaGdBK

var da = new Date();document.getElementById("display").innerHTML = da.toDateString();

//gets the current time. //var d = new Date();var x = document.getElementsByClassName("start")[0].innerText;var z = document.getElementsByClassName("end")[0].innerText;//var o = parseInt(x,10);//var c = parseInt(z,10);
var startTime = Date.parseExact(x, "h:mm tt");var endTime = Date.parseExact(z, "h:mm tt");
if (da.between(startTime, endTime)){ $(".open").show(); $(".closed").hide();}else { $(".closed").show(); $(".open").hide();}
.open, .closed {  display: none;}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script><span class="day">Monday </span><span class="start">8:30 am</span> - <span class="end">5:30 pm</span><br><span id="display"></span><div class="open">Shop is open</div><div class="closed">Shop is closed</div>


Related Topics



Leave a reply



Submit