MYSQL: Inserting time period OR time/moment - mysql

I want to build a little calendar system for my website.
When the user creates an appointment he can choose between a time period (like 3rd march to 7th march) and a concrete day with time/moment (like 3rd march 2016 11:00).
I want to insert this into the mysql database.
Values for inserting a time period:
date1: 3rd march 2016
date2: 7th march 2016
Values for inserting a concrete day with time:
datetime: 3rd march 2016 11:00
Now the question I stuck on: How should the table look like?
I thought on sth like this (only the columns):
id | appointment | date1 | date2 | datetime1
And when the user inserts a time period the datetime-field would be empty. But is this the way to go?

One way would be
id
appointment
startdate
starttime (empty if it is an entry for a whole day)
enddate (empty if it is a single entry without a period)
endtime (empty if it is an entry for a whole day)

Related

How do I create a DATE that starts and ends at 0600 every day

My database collects data and stores in a database with date stamp (in fact two columns one date and another time. now my requirement is to have another column called date (just two digits of date ranging from 01 to 31) but the date should be changing at 06:00 hours instead of 00:00. so for example if date is 14th of Dec and time is 05:30, the DATE should return as 13 and should change to 14 only after 06:00 on 14th Dec. similarly if the date is 15th Dec and time is 03:00 , it should show 14 and not 15
please help in building a sql syntax to achieve the same.
Thanks in advance
Manjunath S

mysql select on datetime objects within range

I have a table 'appointments' that contains, among other things, two datetime fields labeled 'start' and 'end'. I also have date in local time that is converted as a range of a full day into UTC (which is what the SQL table stores the datetimes as). I need to select all the (business) times between 00:00:00 and 08:00:00 UTC that also fall in the range of my local time conversion.
An example, A user in PST (pacific standard time) picks December 1st, 2018. The dates between December 1st at 00:00:00 and December 2nd 00:00:00 are converted to UTC which would be December 1st 08:00:00 to December 2nd 08:00:00. I need to select all appointments between 00:00:00 and 8:00:00 any given day in the previous range (dec 1 - dec 2).
All of my datetimes/queries are in the form 'yyyy-MM-dd HH:mm:ss'.
I know that I can select all of the times between two times rather simply like so:
SELECT start, end
FROM appointment
WHERE start>='2018-12-01 00:00:00'
AND end<='2018-12-02 08:00:00'
But I'm unsure as to how to trim these down to only between business hours.
I'm looking for something like
SELECT start, end
FROM appointment
WHERE (start>='2018-12-01 00:00:00'
AND end<='2018-12-02 08:00:00')
AND (start.substring(11, start.end) >= '00:00:00'
AND end.substring(11, end.end) <= '08:00:00')
Where a call like start.substring(11, start.end) would return the time in 'HH:mm:ss' format
Try using the TIME function in MySQL.
SELECT start, end
FROM appointment
WHERE TIME(start) >= '00:00:00'
AND TIME(end) <= '08:00:00' AND ... //other conditions

How to design table structure for Operating Hours

Problem #1:
How can the table be designed in MySQL in efficient way to structure
1) Store Opening Time,
2) Break Time and
3) Closing Time in a day
For an instance, On Sunday: Opening Time: 7:00AM, Closing Time: 5:00PM, Break Time Start: 1:00PM, Break Time End: 2:30PM.
Problem #2:
How to define the query to search that table for all Stores that are being Opened/Closed between the given range.
For an instance, If user tries to filter the stores for Opening between 9:00AM and 12:00AM. How can I apply query for this filter since it might be possible the Break Time or Closing Time could be within that timeframe.
store_id day open_time break_start break_end close_time
1 mon 7:00AM 1:00PM 2:30PM 5:00PM
1 tue 7:00AM 1:00PM 2:30PM 5:00PM
1 wed 7:00AM 1:00PM 2:30PM 5:00PM
1 thu 7:00AM 1:00PM 2:30PM 5:00PM
1 fri 7:00AM 1:00PM 2:30PM 5:00PM
1 sat Closed
1 sun Closed
Something similar to this. If the user uses criteria: Open Time between: 8:00AM to 2:00PM; to search the store then He must be able to see all the stores that are opened during that timeframe. This timeframe must also include with store open_time. That is, based on criteria provided, the store is Open at 7:00AM, 8:00AM and 9:00AM and until 2:PM.
Better to create three different columns, since you want to query between
Opening time , closing time or break time
create table store_timing
(
store_id int, -- from store table
Opening_Time datetime,
break_time datetime,
closing_time datetime
)
Query :
select *
from store_timing
where Opening_Time<= 'Opening_Time'
and closing_time <= 'closing_time'

My SQL query to list dates that have no records

I have an online calendar system that I use for tracking my band's gigs - I'd like to construct a query that will display all Fridays and Saturdays that don't currently have a record assigned to them.
eg,
if I have a record in the DB for Friday 23rd Aug and Friday 30th Aug (records being gigs that are booked), what would the query cirteria be to output Saturday 24th Aug (as it has no record)?
Select * from ['giglist']
where ['gigdate'is in 'friday','saturday']
and ['gigdate' doesn't have a record]
I will probably set the days of the week as variables so that the user can run the query for any day or selection of days.
Thanks,
Darren
if assuming from your question there is a field gigdate of date type that keeps date information and a seperate record field.
Then query would be,
select DAYNAME(gigdate), DAYOFMONTH(gigdate), MONTHNAME(gigdate) from giglist where
DAYNAME(gigdate) in ('Friday', 'Saturday') and
recordfield is NULL;
It's better to use single date type field and just store date only, as mysql has powerful set of date functions to help you out for your needs.

Best way to store weekly event in MySQL?

I have a table of weekly events that run on certain days of the week (e.g. MTWTh, MWF, etc.) and run on a certain time (e.g. 8am-5pm). What's the best way to store day of week information in MySQL to make retrieving and working with the data easiest? My CakePHP app is going to need to retrieve all events happening NOW().
For time of day, I would just use TIME. For days of the week, I had considered a 7-bit bitfield, a varchar ("MTWThFr" type deal) for the days of the week, but both of those seem like clunky solutions (the varchar being clunkier).
Any suggestions?
Here's a straightforward way:
EventID Title Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1 MyEvent 0 0 0 1 0 0 0 14-01-2010 14-01-2033
How to use:
Simply set a 1 on the days you want to run it. Since the 7-days calendar is not likely to change any time soon, that structure should be immutable. You can choose any combination of days.
To recap:
Run every Thursdays:
EventID Title Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1 MyEvent 0 0 0 1 0 0 0 14-01-2010 14-01-2033
Run every Thursdays & Mondays:
EventID Title Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1 MyEvent 1 0 0 1 0 0 0 14-01-2010 14-01-2033
Further more, you get only one row per event schedule, which is easier and cleaner to handle programmatically.
For example, to find all events to be executed on monday, do:
select * from Events where Mon = 1
Can you add an DayOfWeek column in your table and make it an int? Valid values for that would be 1 thru 7. You could add a constraint on that to enforce that rule. For time, how about a BeginTime columns and an EndTime column? They would be int's as well 0-24
For an event at 5:00 pm on Monday would look like this in your table
Event_ID DayOfWeek BeginTime EndTime
1 2 1700 1800
Why not have several lines, each line having only one column containing the day of week. This column would be just a simple :
ENUM("Monday", "Tuesday", ...)
Then, in PHP you could use date and strtotime functions to get the name of the day :
echo date('l');
echo date('l', strtotime('mon'));
// Outputs "Monday"
It is way more readable to store the name of the day.
In case anyone coming this way again I am using SMTWHFA quite efectively with a simple string search.
So Monday Wednesday Friday would be MWF and Sunday Monday Thursday would be SMH. Takes a while to get used to H=Thursday (second letter) and Saturday=A but it works!
Since there won't be any new days of the week invented (I hope!) you could just create a bool column for each day. Then, if you are running a query to find events on a Friday, it would simply be (with a bit of pseudocode):
SELECT eventName
FROM events
WHERE fridayBool = true
AND eventStartTime < NOW()
AND eventEndTime > NOW();
In that example the name of the column you would store in an array in your code, and check today's date to see what day of the week it is, which then selects the proper name out of the array before creating the query.
Not the most elegant, but it should work.
Edit...
Example table columns:
eventID
eventName
eventStartTime
eventEndTime
sundayBool
mondayBool
...
saturdayBool
Break the weekly information into a separate table entirely:
events: id, beginTime, endTime, name, description
eventsbyday: id, dayofweek, event_id
This will allow you to query eventsbyday according to the current day of the week:
SELECT events.name, events.beginTime, events.endTime FROM eventsbyday JOIN events ON eventsbyday.event_id=events.id WHERE dayofweek=0
This is very rough code, but the idea is to break out the weekly information, allowing you to have the same event associated with multiple days of the week.