Conditional Statement to Trigger Open/Close for Store Hours

Hi, I have a list of stores that have open/close times for each day of the week. Based off what the store hours are, I’ve created conditional statements for a text that will say ‘Open’ if the user’s current time is within the store’s open to close time. The opposite will happen if the user’s current time is outside of the open to close time.

The conditional statement I’ve set keep saying a store is ‘Closed’ even though the store should be ‘Open’, based on the user’s time.

Here’s what the conditions are for the text (first 3 are open/close conditions that I need help on, last 2 are work as they should):



This is the backend vs front-end of the store’s hour table:


As you can see, the user’s time is 5:21pm but the text shows ‘Closed’.

Really appreciate any help given, thanks! :slight_smile:

1 Like

The problem to work with “Current date/time” is that you are using the DATE as well.

How you stored the open and close hours in your database? Could you share?

@rpetribu Ah that makes so much sense! Is there a way to just include dates or is that through a plugin?

Here’s the data type for hours:

Hi!

Try to store the open and close hours as number (in minutes) in your database.

Sumday 08:00 to 20:00 will be Sunday 480 to 1200.

08:00 = 8 x 60 = 480
20:00 = 20 x 60 = 1200

Now, just check the weekday of your user, using Current Date/Time, find this entry in your database and divide the Current Time by 60. If the result is between 480 and 1200, then your store is open!

Make sense? :grin:

1 Like

@rpetribu I get the logic behind this but I can’t seem to figure out ways to execute this in Bubble.

I’m facing three issues:
1. I inputted hours to be 480 to 1260, and when I divided these values by 60, I’m still seeing 480 and 1260 on the front-end side. How do I correctly create the formula to show 8:00am and 9:00pm?

Back-end:


Front-end:

2. How do I create another formula to divide the user’s current time by 60? (I do not see the division sign available)

3. If I’m still taking in user’s Current date and time, how does the date not mess up this new workflow and accidentally mark a store as closed?

Hi @sydneyng !

What I would do is:

  1. Create a database to store the data of each Store (think you already did it). In this database, I would have 4 fields to control this “time” thing.
  • Open_hour_text: 08:00
  • Open_hour_number: 480
  • Close_hour_text: 20:00
  • Close_hour_number: 1200
  1. In my Store’s page, in the text filed that displays the opening and closing hours, I would simply get the “Open_hou_text” and “Close_houre_text”. Simple as that…

  2. Now, to dynamically toggle the sign “Open/Close” based in users Current Date/Time you will need extrat the hours and the minutes:

Current Date/Time:Extrat Hours*60 + Current Date/Time:Extrat Minutes

It the time is 14:40, then the result of this operation will be: 880, follow?
If 880 is between Open_hour_number and Close_hour_number , than your store is open.

Now, do a “Search for” to see if you get any result from you database with this constrains:

  • Store = Current Page Store
  • Weekday = Current Date/Time Weekday
  • Open_hour_number < Current Date/Time:Extrat Hours*60 + Current Date/Time:Extrat Minutes
  • Close_hour_number > Current Date/Time:Extrat Hours*60 + Current Date/Time:Extrat Minutes

If the :count of this Search is > 0, than your store is open.

6 Likes

@rpetribu followed your steps and it worked! Thanks so much :slight_smile:

1 Like

Would this method work if the hours were like this:

Saturday: 5pm - 2am

if the time were 7pm, the open hour number would be less than the extracted hours + extracted minutes, but the closing hour would also be less than the extracted values, so the search would return empty.

1 Like

Got any solution?

If your closing hour is lower than your opening hour, you will need another logic with two separated searches:

Search 1:

  • Open_hour_number < Current Date/Time:Extrat Hours*60 + Current Date/Time:Extrat Minutes
  • Close_hour_number < Current Date/Time:Extrat Hours*60 + Current Date/Time:Extrat Minutes

Search 2:

  • Open_hour_number > Current Date/Time:Extrat Hours*60 + Current Date/Time:Extrat Minutes
  • Close_hour_number > Current Date/Time:Extrat Hours*60 + Current Date/Time:Extrat Minutes

Search1:count + Search2:count

If the COUNT of both searches, summed, is >0 then your store is open.

In your example:

Current time = 7pm = 19:00 = 19 * 60 = 1140
Opening hour = 5pm = 17:00 = 17 * 60 = 1020
Closing hour = 2am = 2:00 = 2 * 60 = 120

Search 1 count is 1
Search 2 count is 0
Store is OPEN :white_check_mark:


Current time = 1am = 01:00 = 1 * 60 = 60
Opening hour = 5pm = 17:00 = 17 * 60 = 1020
Closing hour = 2am = 2:00 = 2 * 60 = 120

Search 1 count is 0
Search 2 count is 1
Store is OPEN :white_check_mark:


Current time = 3pm = 15:00 = 15 * 60 = 900
Opening hour = 5pm = 17:00 = 17 * 60 = 1020
Closing hour = 2am = 2:00 = 2 * 60 = 120

Search 1 count is 0
Search 2 count is 0
Store is CLOSED :x:


Hope it helps!

3 Likes

Hey,
how can I set it up so that when the time (opening_time_text) is set to 08:00, my opening_time_number is automatically set to 480? I have created the different options, but I don’t know how to link them together.

Hey @lemw.business :wave:

I am with my phone now so sorry if I type something not so accurate here :sweat_smile:

You can use the expression below :point_down:

(opening_time_text: split by “:” :first item :converted to number * 60) + (opening_time_text: split by “:” :last item :converted to number)

See if it helps!

2 Likes

You’re great, thank you so much! :slight_smile:

1 Like