Hello friends,
I was hoping that I was at the point now that I would be answering questions in this group but alas, I have found myself stumped on what (I’m thinking) should be a fairly simple task. Hoping to get an experts eye on this to point out where I’m missing things.
The Outcome:
I’d like to dynamically display store hours based user configured settings. Users have a setting that contains open and closed hours for each day of the week (which is already working and handled elsewhere). Easy enough right? The kicker is, I’d like to take any days where the hours are the same and essentially eliminate them, so that instead of displaying: “Sunday: 8am-8pm, Monday 8am-8pm, Tuesday 8am-8pm, Wednesday 8am-8pm, Thursday 8am-8pm, Friday 8am-10pm, Saturday 8am-10pm” it would display “Sunday - Thursday 8am-8pm, Friday - Saturday 8am-10pm”.
Note
Since the hours are user configurable, this information is subject to change, meaning that it could be “Sunday - Tuesday 8am-8pm, Wednesday: 8am-9pm, Thursday - Saturday: 8am-10pm”…for example or any combination thereof.
The Code
$days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
$hours = [ ["8:00 am","9:00 pm"], ["8:00 am","9:00 pm"], ["8:00 am","9:00 pm"], ["8:00 am","10:00 pm"], ["8:00 am","9:00 pm"], ["8:00 am","9:00 pm"], ["8:00 am","9:00 pm"], ];
for ($x = 0; $x < 6; $x++) {
$db = $x - 1;
if ($hours[$x][0] == $hours[$db][0] && $hours[$x][1] == $hours[$db][1]) {
// The hours are the same skip this
}
else {
// The hours don't match, add them to the list
}
}
The Summary
I know that the code above is overly simplified but I wanted to both save myself the embarrassment of showing some of the things I’ve been trying as well as get the point across without overcomplicating things.
Since the scope is confined to the 7 days a week, I suppose this could be done with a ton of conditionals accounting for every possible configuration but… that just wouldn’t make me feel good.
I’m wondering if there is either a library that has solved this or perhaps even some magical array manipulating function that could help out here?
Keep in mind that the hours and days of course can be manipulated any way needed to make this work since I’m pulling it in manually.
Thanks for any thoughts on how best to accomplish this!