Save to the database the office hours and perform a query

Asked

Viewed 1,711 times

4

Hello, I am looking for a North to create with PHP and Mysql a way to record in the database the hours of operation of several establishments, to be able to easily search and show if the establishment is open, or closed, or until hours on the clock on that day.

Something like the example below I intend to do, same as Google Maps.

Exemplo do resultado final pretendido

  • What would be your doubt?

  • I’m having trouble coming up with the part where the opening and closing hours of the establishment will be, I create something [ID - COD_EMPRESA - DIA_SEMANA - HORA_ABERTURA - HORA_FECHAMENTO ]?

  • I think it would be better this way... [ID - COD_EMPRESA - DIA_SEMANA - HORA_ABERTURA_MANHA - HORA_FECHAMENTO_MANHA - HORA_ABERTURA_TARDE - HORA_FECHAMENTO_TARDE ]... Because you have 2 schedules for each day of the week... Or you can do the way you said, just create another column, for example, [Periodo] and define it as morning(1) or afternoon(2), so you can differentiate the 2 times on the same day of the week...

  • Good, I will implement something like this, since some places have different hours (work late and night) [ ID - COD_EMPRESA - DIA_SEMANA - HORA_ABERTURA - HORA_FECHAMENTO - PERIODO ]

  • I ask? are many registers (establishments) to include a timetable?

1 answer

2

You can implement this functionality as follows:

Structure of the database

For your database table structure, you can create something like this:

[ID - COD_EMRPESA - DIA_SEMANA - HORA_ABERTURA - HORA_FECHAMENTO]

Thus, the example company in the question would have 11 records in the database: 2 for each day from Monday to Friday and 1 for Saturday.

I do not think it is necessary to determine the period. What would be the period of a company that is open direct from 09:00 until 18:00 for example?

Storing the information in the bank

Based on the documentation of the date function() (http://php.net/manual/en/function.date.php), you would store the information of each company in the database as follows:

  • the days of the week (DIA_SEMANA) from 1 to 7 (being 1 Monday and 7 Sunday).
  • opening/closing times (HORA_ABERTURA and HORA_FECHAMENTO) as HH:MM (for example 14:03 or 17:10).

Checking opening and closing times

First, to get the day of the current week write:

$dia_da_semana = date('N')

And to get the current time and minute:

$horario = date('H:i')

Then, just compare the strings returned above with the values present in the database to determine whether or not the company is open.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.