Handling Shops Open and Close time - php

What is the best way to store shop opening and closing time in the database and also how to calculate the time in PHP?
I have come up with this table design:
+----+---------+----------+-----------+------------+
| id | shop_id | week_day | open_hour | close_hour |
+----+---------+----------+-----------+------------+
| 1 | 3 | 1 | 15:00:00 | 23:00:00 |
| 2 | 3 | 2 | 15:00:00 | 23:00:00 |
| 3 | 3 | 3 | 18:00:00 | 02:00:00 |
| 4 | 3 | 4 | 18:00:00 | 02:00:00 |
| 5 | 3 | 5 | 18:00:00 | 03:00:00 |
+----+---------+----------+-----------+------------+
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| shop_id | int(11) | NO | | NULL | |
| week_day | int(11) | NO | | NULL | |
| open_hour | time | NO | | NULL | |
| close_hour | time | NO | | NULL | |
+------------+---------+------+-----+---------+----------------+
For example, on Tuesday (week_day = 2) it open at 3PM and close at 11PM (Tuesday).
On Wednesday (`week_day = 2'), it open at 6PM and close after midnight at 2AM which would be Thursday. Should after midnight time saved in same row?
Let say customer want to place an order (shop_id = 3) at 10PM on Tuesday, they should be able to do so according to the database data. However if customer want to place an order at 1AM on Thursday but the database show that week_day = 3 it close at 02:00:00
How to write in PHP to work out if the shop open or not?
Do I need to change the the table design so it would much easier to write in PHP?

Try this
http://php.net/manual/en/book.datetime.php
with this
http://php.net/manual/en/class.dateinterval.php

You can say that if close_hour is less than open_hour for a given entry that the opening hour is in the next day.
Or you could use separate week_day columns for opening and closing time.

Related

Change a row header based on query results without doing multiple queries

I just can't logically think how to do this without doing multiple queries, which I'd like to avoid if possible. I have a mysql table set up like this.
+------+----------+--------------+--------------+
| Id | Opponent | Game Date | Playoffs |
+------+----------+--------------+--------------+
| 1 | Chicago | 2020-08-20 | First Round |
| 2 | Chicago | 2020-08-22 | First Round |
| 3 | Chicago | 2020-08-24 | First Round |
| 4 | Chicago | 2020-08-26 | First Round |
| 5 | Dallas | 2020-08-28 | Second Round |
| 6 | Dallas | 2020-08-30 | Second Round |
| 7 | Dallas | 2020-09-01 | Second Round |
+------+----------+--------------+--------------+
What I want to do is pull the results so I can display them in a table like this.
+------+-------------------+----------------+
| Game | Opponent | Date |
+-------------------------------------------+
| Playoff Round: First Round |
+-------------------------------------------+
| 1 | Chicago | 2020-08-20 |
+------+-------------------+----------------+
| 2 | Chicago | 2020-08-22 |
+------+-------------------+----------------+
| 3 | Chicago | 2020-08-24 |
+------+-------------------+----------------+
| 4 | Chicago | 2020-08-26 |
+-------------------------------------------+
| Playoff Round: Second Round |
+-------------------------------------------+
| 5 | Dallas | 2020-08-28 |
+------+-------------------+----------------+
| 6 | Dallas | 2020-08-30 |
+------+-------------------+----------------+
| 7 | Dallas | 2020-09-01 |
+------+-------------------+----------------+
I just can't quite think how to do it without running separate queries on each round. I'd rather not have to do it that way, I think it's probably unnecessary. Thanks!

How to Subtract Time between two Tables

I'm working on an event system and would like to know how to get the range of time available to schedule. I have the following tables
Table1 (To record the availability of creating events)
| Date | Start Time | Final Time |
| ------------- |: -------------: | ----------- |
| 2019-02-01 | 09:00:00 | 12:00:00 |
| 2019-02-01 | 13:00:00 | 17:00:00 |
and then I have Table2 to properly record the events, taking into account the availability in Table1. Table2 and Table1 have the same fields. An example of the records in Table 2 would be the following:
Table2
| Date | Start Time | Final Time |
| ------------- |: -------------: | ----------- |
| 2019-02-01 | 08:00:00 | 11:00:00 |
| 2019-02-01 | 13:00:00 | 14:30:00 |
The specific question is: What would be the SQL query to be able to determine what are the remaining time ranges, available from Table1 with respect to those that have already been taken or set out in Table2.
I'm using Laravel but I can not get the exact SQL query that works for me in this case.
I would like to get this result:
| Date | Start Time | Final Time |
| ------------- |: -------------: | ----------- |
| 2019-02-01 | 08:00:00 | 09:00:00 |
| 2019-02-01 | 11:00:00 | 12:00:00 |
| 2019-02-01 | 14:30:00 | 17:00:00 |
I hope I was clear. Thanks in advance.

select and calculate schedule dates then put them into an array

I have a table with posts I want to schedule on Facebook. No schedule dates are stored here.
I have a second table with schedule dates based on week days. All hours are the same but not all weekdays are included. Some of them are missing.
| id | mon | tue | wed | thu | fri | sat | sun |
| --- | ----- | ----- | --- | ----- | --- | ----- | ----- |
| 1 | 10:20 | 10:20 | | 10:20 | | 10:20 | 10:20 |
| 2 | 15:25 | 15:25 | | 15:25 | | 15:25 | 15:25 |
| 3 | 18:25 | 18:25 | | 18:25 | | 18:25 | 18:25 |
| 4 | 21:25 | 21:25 | | 21:25 | | 21:25 | 21:25 |
| 5 | | | | | | | |
| 6 | | | | | | | |
| 7 | | | | | | | |
Let's say we are in the first day of the week (08:00) and I want to calculate the dates for the next 100 posts (5 weeks from now). I'm thinking I ought to get an array with all these dates in UNIX time.
$array = array('1439893200','1439896200','1439833200',......'1437893200','1449893200');
I need the array because I want to output the dates when I display the posts from the database. The posts can be reordered and the dates can be changed that's why I don't store the schedule dates there. I prefer a separate table for that.
I already spent a lot of time trying to achieve this but without success. Any starting advice and help will be appreciated.
How about we start in Wednesday when there are no scheduled hours?

PHP Laravel view table design

I've been working on a project where I have 3 tables of dates, times and usernames and when I join them the last view is:
same date - same time - diff username
same date - same time - diff username
diff date - diff time - diff username
An example with real data would be:
2014-01-03 | 10.08 | alexrussell
2014-01-05 | 11.15 | geartz
2014-01-05 | 14.23 | geartz
2014-02-02 | 13.44 | alexrussell
2014-02-02 | 13.44 | geartz
2014-02-02 | 18.21 | alexrussell
What I'm trying is to create a table where the data that's the same from row to row is grouped:
-----------------------------------
| Date | Time | Username |
|------------|-------|--------------|
| 2014-01-03 | 10.08 | alexrussell |
|------------|-------|--------------|
| | 11.15 | geartz |
| 2014-01-15 |-------|--------------|
| | 14.23 | geartz |
|------------|-------|--------------|
| | | alexrussell |
| | 13.44 |--------------|
| 2014-02-02 | | geartz |
| |-------|--------------|
| | 18.21 | alexrussell |
-----------------------------------
I have 1 object and I'm using foreach to parse it. I tried several selects with distinct but that way, I can't include other objects in table so code gives error like 1 select with distinct for date and 1 for the rest which have "where" statement. I tried 1 object with several arrays in it but again since I'm using foreach, I can only fill 1 column and next column requires other object.
I also tried giving a rowspan per object count but It didn't work since I can't exactly get the user count.
There must be a way and I'm totally stuck on this. Probably I'm not the first one encountering a problem like this.

Select rows from specific date with left join

I'm currently working on a simple employee scheduling tool and have a problem with a SQL query. To explain my problem let use this two very abstract tables.
The first table simply consists of the employees
employees
======================
empId | name | ...
----------------------
10 | Scott |
11 | Schrute |
12 | Halpert |
13 | Howard |
In the second table you find the assigned tasks to each employee by day.
tasks
==============================================
tasId | name | task | date | ...
----------------------------------------------
10 | Scott | Support | 2014-02-17 |
11 | Scott | Bugfix | 2014-02-18 |
12 | Halpert | Bugfix | 2014-02-17 |
13 | Halpert | Develop | 2014-02-18 |
14 | Howard | Support | 2014-02-17 |
Now I want to know what the employees are working on on Feb 17th or if they have no tasks planned for that day. I use the following SQL query to do that.
SELECT e.name, t.task
FROM employees e LEFT JOIN tasks t ON e.name = t.name
WHERE date IS NULL OR date = DATE('2014-02-17')
The result delivers exactly what I need:
name | task
--------------------
Scott | Support
Schrute | NULL
Halpert | Bugfix
Howard | Support
And now to my problem. If I want to see the tasks of Feb 18th I get this result set:
name | task
--------------------
Scott | Bugfix
Schrute | NULL
Halpert | Develop
The reason to this is obvious, the date of Howard's tasks are neither NULL nor do they equal 2014-02-18.. What would be the best way to get the desired result?
I use MySQL and PHP.
(Sorry for the stupid title, I couldn't think of anything better..)
Move your filter to the join predicate:
SELECT e.name, t.task
FROM employees e
LEFT JOIN tasks t ON e.name = t.name AND t.date = DATE('2014-02-18');
Your query as it is will only return people who have no tasks at all, or have tasks on the date specified. It will omit people who have tasks, but not on the date specified. Consider the results of joining your sample data with no where clause:
empId | name | task | date | ...
----------------------------------------|
10 | Scott | Support | 2014-02-17 |
10 | Scott | Bugfix | 2014-02-18 |
11 | Schrute | NULL | NULL |
12 | Halpert | Bugfix | 2014-02-17 |
12 | Halpert | Develop | 2014-02-18 |
13 | Howard | Support | 2014-02-17 |
As you can see there is no record for Howard where Date = 2014-02-18, or where Date is null, this is why no record is returned for Howard. When you add the filter to the join predicate your results become:
empId | name | task | date | ...
----------------------------------------|
10 | Scott | Bugfix | 2014-02-18 |
11 | Schrute | NULL | NULL |
12 | Halpert | Develop | 2014-02-18 |
13 | Howard | NULL | NULL |
Which I think is the desired results.
You can use
UNIX_TIMESTAMP(`date`) = 0
Looks like you have some data which are stored as '0000-00-00' and its not null so adding the extra OR condition as above will return those data as well.

Categories