PHP, MySQL Recurring Slot in Calendar using Daily, Weekly, Monthly, Yearly - php

I have this problem on how can I populate the slots in database if I need to look in another table and if there is a recurring so I need to repeat that slot without even adding it, just showing the slot again in another date.
For example, I have this calendar.
Calendar
As you can see in February 14 there's 1 Time Slots. And that slot has a recurrence Daily separate in another table. How can I show that time slot in another date also like February 15,16,17..29.
mysql_query("SELECT client_contact_slots.*, client_contacts.*, client_recurring_slots.*
FROM client_contact_slots
JOIN client_contacts ON client_contacts.client_contactid = client_contact_slots.client_contactid
LEFT JOIN client_recurring_slots ON client_recurring_slots.clientcontactslotsid = client_contact_slots.slotid
WHERE client_contact_slots.client_contactid in ($ci) and client_contact_slots.date in ($dstring)
ORDER BY client_contact_slots.slotstart ASC");
The recurrence is in table client_recurring_slots and with column recurring that will only have value Daily, Weekly and Monthly, Yearly.
Can somebody help me?
Thank you.

Related

Schema for appointment booking system using MySql

I am building a doctor appointment booking system for a single doctor using MySql.
A patient will have to select a date and choose a time-slot .Each hour should be divided into 6 slots Each slot should have only 10 minutes to it.
Also a particular date and its slots can be locked if the doctor is busy on that particular day.
Please help me design a schema for the table in which a customer can book a slot .

MySQL how to select all rows by specific day (mon, tue, etc) with datetime format

I have an event system setup where a user can add an event for say Monday and it can either occur once or it can be a reoccurring event. So in my application whenever the users requests to see all events for Monday I want to return any events that are on that day and also return any reoccurring events that are on Monday but could have been added in the past.
I'm not sure how the MySQL query should look. In the database I have these fields for the event.
id, title, day (DATETIME), reoccurring (TINYINT) 0/1
Thank you in advance for any guidance you can offer.
You can use the DAYNAME() function in your query.
SELECT *
FROM table
WHERE day = currentDay
OR (
DAYNAME(day) = DAYNAME(currentDay)
AND reoccuring = 1
)
For more examples of DAYNAME:
http://www.w3resource.com/mysql/date-and-time-functions/mysql-dayname-function.php

Calendar with time slot reservation

I'm very new in this site. Spend a lot of time for searching an answer for my target, but unsuccessfully. So, there it is (sorry for my english):
I'm quite new in php, but already created nice stuff. But now I'm interested in calendar with time slot reservation. I saw good exemple then I registered for my doctor. There i was able to choose a day from calendar and then select a free time range (e.g. from 12:00 to 12:30).
First idea for this is some calendar in PHP and table in MySQL. In MySQL table few rows with id, name and quantity. id - unique number of row, name - date and time slot, and quantity - (1 = free cell, 0 = unable). In PHP calendar after selecting a free time slot, in DB quantity goes from 1 to 0 and apear unavailable for other users.
But if where are a lots of "doctors"? How to make calendar, DB for thousands of them?
For the beggining maybie someone have some example how to make calendar with time reservation (e.g. 1 hour) in the easiest way? Or suggest something?

insert select statement with changes to fields and logic thinking

I'm working on some php code having to do with creating recurring invoices. I need some help thinking through the logic. I think ultimately I will end up with a type of Insert/Select statement. The php code that is ultimately generated here will run in a nightly cron.
First, let me show you some of my table items for a frame of reference and explain a bit about what they do. The table is called Invoice and there are other tables associated with it, but I think if I can nail now this one table's logic, I can apply it to the others.
ID = This is the primary key that auto increments and also happens to be the invoice number.
recurring = This is whether the invoice is one time or recurring. O=One time, M=Monthly etc
recurred = This will be set to N on insert and is the logic I'm using to see if the next recurring invoice that will be created in the table should be based off this invoice or not. When a new recurring invoice is inserted into the table, this will be changed to Y so its not processed again by the cron job.
date = This is the date that the invoice was inserted/created. For example, if date the invoice was created is the 15th of this month, the next invoice created will be the 15th of next month. Essentially M represents monthly. However, I haven't figured out yet what to do about a created date of the 31st but the next month only has 30 days, could use some help here.
So here is my logic. First, I run a query to find all rows that are not O and recurred = N.
$result = mysql_query("SELECT * FROM invoice WHERE recurring != 'O' and recurred
= 'N'") or die(mysql_error());
Next, I work on each kind of recurring M for monthly, Q for quarterly, etc..
if recurring = M
if todays date day = 'date' day
copy line item into a new row with the following changes: new ID, todays date, and mark the recurred to N for the new insert (will mark the old one Y)
Obviously that is not actual code, just what I'm thinking about.
So my questions are 1.) How to create a insert select statement to deal with changed variables and 2.) how to deal with dates where if the invoice was created on the 31st of one month and the next month only has 30 days (or 28).
take only rows that need to be re-inserted:
SELECT *, DATEDIFF(`date`, NOW()) as diff FROM invoice WHERE recuring != 'O'
AND recured = 'N' AND diff >= 30;
iterate through that list and use a new date for each row by calculating it with:
$newDate = date("Y-m-d", strtotime($oldDate+" +1 month"));
This should solve your 30th day of month problem.
Now insert newly created data.
Also, good advice to use enum for recuring field, and boolean for recured.
I have made somewhat similar application. In my case I had 2 tables: actual invoices and recurring invoices. Recurring table had one field called NextDate - the date when next invoice should be created. One time invoice is added directly into invoices table, otherwise the cron job scans the recurring table and checks for a NextDate value. If it is in past then new invoice is generated into invoices table and NextDate is updated (normally incremented by month).
In my case incrementing by month changes month value only by 1. For example in MySQL
DATE_ADD('2008-01-31', INTERVAL 1 MONTH) --> 2008-02-29

Counting records for particular ranges dates and times

I have a database that stores details of money-off vouchers generated on our online system, there are four specific voucher types, and each time a voucher is generated, it stores a record in the database with a DATETIME and the voucher type.
I have to create a very specific spreadsheet report for a client that accepts two dates as ranges (so they say "generate a report between these two dates"), and then goes through EVERY DAY between those two ranges, and reports how many vouchers of that type were generated per to the closest hour. So a sample left hand column would look like this:
27/7/2010 07:00
27/7/2010 08:00
27/7/2010 09:00
27/7/2010 10:00
if there are no vouchers generated during a certain hour, it isn't shown on the spreadsheet.
Basically, i need some help figuring out how to generate this! i can generate a .CSV file, that's the simple part in my opinion. Apart from that, i don't have a clue where to begin.
SELECT DATE_FORMAT(YourDateColumn, '%d/%M/%Y %H'), Count(Vouchers)
FROM Table
WHERE YourDateColumn between StartDate and EndDate
GROUP BY DATE_FORMAT(YourDateColumn, '%d/%M/%Y %H')
ORDER By 1

Categories