PHP/MySQL - Calender/Setting appointments question - php

I have to columns, both datetimes. One's for the time the appointment starts and the other is for when the appointment ends.
I need a mysql query that'll basically return available time during the day so I can set appointments without them overlapping.
So if I'm open on Monday from 1pm to 6pm and have an appointment from 2pm to 3pm it'll return two results:
DATE 13:00:00 - DATE 14:00:00 and DATE 15:00:00 - DATE 18:00:00

My solution would be to always have 2 datetimes, one being the start time, one being the end time. That would be the 'cleaner' way in my opinion.
The way to get the open appointments is something like this:
SELECT *
FROM appointments
WHERE end_time = "0000-00-00 00:00:00" (or > in stead of = for all appointments)
Or if you leave it empty something like this:
SELECT *
FROM appointments
WHERE end_time IS NULL (or IS NOT NULL for all appointments)

Related

Time Field Management for AM/PM & 24 Hours using PHP/MySQL

I am using two fields in one of my table named as "tbl_TimeTable" in MySQL. Field Names are Start Time and End Time. I need to store Open Time and Close Time using these 2 fields. Now the Problem is Open Time is 09:00:00 AM and End Time is 02:00:00 AM. I can not find my results using this query.
Select TIME_FORMAT(a.End_Time, '%r'), a.Company_ID
from tbl_timetable a WHERE
TIME_FORMAT('15:00:00', '%r') BETWEEN
TIME_FORMAT(Start_Time, '%r') AND TIME_FORMAT(End_Time, '%r')
Table contains:
2 as Company_ID and Start_Time = 09:00:00 and End_Time = 02:00:00

Finding number of times days appear incrementally

I have an application that records user transactions into a database. The data recorded includes: the time a user performed a transaction (e.g. purchasing materials), the day (e.g. 2016-12-23), what the user transacted, etc. I have an admin panel that collects this data and shows the overall statistics. I need to analyse the data such that, for example; if i need to find out all purchases made for that week (i.e.from Sunday to the current day of the week), i select all appearances from the database that have the dates from the immediate previous Sunday until the current day, so that i show a weekly statistics.
Now, since the average is done weekly, i need only to get the current date, that of the previous sunday and the days in between. I know how to get these two but no idea how to get the middle dates depending on the day of the week.
Note that the time i huse is in the format
$today = date("Y-m-d");
and to get the previous sunday i use:
$prevsunday = date("Y-m-d",strtotime("last Sunday"));
I ask how to get the middle days, and how to handle the updated query.
Any suggestions to improve my query?
Assuming MySQL, you can use dayofweek to figure out "previous sunday":
mysql> select curdate(), dayofweek(curdate()),
curdate() - interval (dayofweek(curdate())-1) day AS sunday;
+------------+----------------------+------------+
| curdate() | dayofweek(curdate()) | sunday |
+------------+----------------------+------------+
| 2016-02-23 | 3 | 2016-02-21 |
+------------+----------------------+------------+
1 row in set (0.00 sec)
So you'd have something like
SELECT ...
...
WHERE dateofpurchase BETWEEN
(curdate() - interval (dayofweek(curdate())-1) day)
AND
curdate();

Adding 1 month to a date and still having it on the same week day

I have a date, let's say 2016-01-20 00:00:00. I want to add 1 month to the date, but not in the way that will end me up with 2016-02-20 00:00:00, instead I want it to be on month later on the same week day so basically 2016-02-17 00:00:00.
How can I do that?
EDIT: I basically want it to do the same thing as google calendar can, where you can select, when adding a new date and setting on repeat, you can select either repeat on the 4th monday or repeat on the 26th of that month. I want to know how i can do the first option to a date

Mysql intersection of datetimes

I have the table Vacation in mysql DB. The table has datetime_from and datetime_to. To have a vacation from Monday to Friday means there is only one record in the table with two timestamps
date_from = 'MONDAY_DATE 00:00:00'
date_to = 'FRIDAY_DATE 23:59:59'
The working time is from 8:00 to 16:00 every day. I would like to get all the working time that employee missed during his vacation (in hours for example). It's 8hrs a day x 5.
Im able to do that in 5 queries (one for every day and then sum up with PHP) as an intersection of date intervals BUT is it possible to perform it in only one mysql query?
SELECT SUM(date_to - date_from) as seconds_missed FROM Vacation
WHERE ...
That would give you the total number of seconds missed for an employee assuming the where clause matches it against a given user. I guess you could also add conditions in your where clause to only grab dates in a certain range (i.e. worked missed that week).

mysql date show results today/yesterday/week

I am retrieving data from a table and show the total SUM of entries. What I want to do is to show the total SUM of entries made on today's date, yesterday and this month. The table is using the unix timestamp format (e.g. 1351771856 for example).
Currently I am using this line to show todays results:
AND comment_date > UNIX_TIMESTAMP() - 24 * 3600";
but that gives me just the entries for the last 24 hours.
Example: So let's say its Friday, 17:00 PM - it gives me the count from Thursday 17:00 PM to Friday 17:00 PM
What I want is to get the results for
Thursday 00:00:00 - 23:59:59 (yesterday in this case)
the results for today (00:00:00 - 23:59:59)
and last week, results that start on Monday, 00:00:00 until "today" (in this case Friday).
I couldn't find a way in the MySQL documentation to achieve this.
This mysql code should work for you:
// Today
AND DATE(from_unixtime(comment_date)) = CURRENT_DATE
// Yesterday
AND DATE(from_unixtime(comment_date)) = DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY)
// This week
AND YEARWEEK(from_unixtime(comment_date), 1) = YEARWEEK(CURRENT_DATE, 1)
// This month
AND YEAR(from_unixtime(comment_date)) = YEAR(CURRENT_DATE)
AND MONTH(from_unixtime(comment_date)) = MONTH(CURRENT_DATE)
Simply use this:
AND comment_date > date_sub(current_date, interval 1 day)
See my answer here, I think it's quite related.
Pull records from orders table for the current week
Consider getting intimate with MySQL's GROUP BY. You will most likely need to know this if you use MySQL.

Categories