PHP Calculating next event date - php

I have a database that holds events in the following format:
Schedules
id
show_id
starttime - datetime
endtime - datetime
repeatuntil - datetime
repeat - int
nthdayofmonth - int
repeatmultiple - int
show_id holds the ID number for the show which is stored.
Shows can be stored multiple times for different recurring, or different days/time variations.
startime, endtime, and repeatuntil are bit obvious as to what they do.
repeat is the type of repeat :
1 - hourly
2 - daily
3 - weekly
4 - monthly
5 - yearly
nthdayofmonth am not 100% whether this is needed or will be used
repeatmultiple is to be used when the events are wanted to be repeated every other week or every other month so on...
What I have been struggling with is the code that calculates when the next event occurs, I have tried various solutions from the internet and stackoverflow but am still struggling. Majority of the time my code ends up in an infinite loop and is unable to work out the correct date for the next event.
If someone is able to help out with the coding of a function that can loop through finding the next time an event occurs I will much appreciate there assistances as I am getting very frustrated and not much further from when I started.

I think you can approach this by characterizing the two functionalities that you really need here.
First, you need a method of calculating the next DateTime that a given Show will execute. This is irrespective of any other Show, and has nothing to do with what will come first. You need a method that, given the starttime, endtime, etc criteria, will calculate the next time that this Show will execute.
Once you are able to determine when any given Show will execute, you can move on to determining which Show will execute next. You could loop through the database, calculating Next times and storing the soonest...or even serialize the Next DateTime for each Show, and query the database for the smallest.

Related

How do I create a list of time ranges?

I'm trying to set up a way to be able to step backward and forward through sets of time ranges.
I'm creating a simple display for my work's production floor. Every time a user scans a barcode a serial number is generated in our ERP system. In order to track progress throughout the day they want a display that will show how many serials (and because of that, how many widgets) were processed that day. The display would also show the previous shift's widgets. So at any given time you look at the display you'll see the current shift's progress, and then the previous shift's totals.
We run 3 normal shifts throughout the week. 1st shift 6am to 2pm, 2nd shift 2pm to 10pm, 3rd shift 10pm to 6am. Then on both weekend days we have a weekend shift that runs 6am to 6pm.
I currently have a spaghetti noodle mess of if/else statements that make this actually work, but I really want to clean it up and make it easier to maintain long-term.
So let's say it's Monday 1st shift. I want to grab Monday 1st shift's totals, but I also want to step backwards by 1 shift to the weekend shift and grab those totals to display. The weekend shifts are really what threw this for a loop because now I have to be aware of the day of the week. 3rd shift is a little leery as well because it crosses over into multiple dates. Is there a clean way to set this up? A library out there that lets me define these things easily?
Hope this made sense. Happy to clarify anything I'm missing. Thanks in advance for any guidance!
Maybe a shifts resource would be usefull.
creating a shifts table with id, start_date, end_date, shift_type,...
when scanning for a barcode:
if the current shift was already created, assign a shift_id to the barcode row
if not, create the shift row, and assign a shift_id to the barcode row
Benefits:
Very easy and fast query to get the results that you need
If shifts changes (starting hour, ending hour,...) it would still work (the only change needed is on the method that creates the shift row)
Comparing shifts results would be very easy
Have you tried using CarbonPeriod? Note that Carbon is already included in Laravel so there's no need to go and add it with Composer.

Building a scheduler with sql, php. Most efficient way

I am creating a system that requires a schedular for a particular task. Users may pick from times 24 hours a day, 7 days a week.
I came up with a few options for the database storage, but I don't think either one is the most efficient design, so I'm hoping for some possible alternatives that may be more efficient.
On the user side I created a grid of buttons with 2 loops to create the days, and the times, and I set each a unique value of $timeValue = "d".$j."-t".$i;
So d1-t0 will be Saturday at Midnight d3-t12= Tuesday at Noon, and so forth.
So, in the database I was first going to simply have a ID, day, time set up, but that would result in a possible 168 rows per event
Then I tried an ID, day, and time 0-23 (a column for each hour of the day) And I was simply going to have a boolean set up. 0 if not selected, 1 if it is.
This would result in 7 rows per event, but I think querying that data might be a pain.
I need to perform a few functions on this data. On each day, list the number of selected times into an array. But I don't believe having a select statement of SELECT * from schedule where time0, =1 or time1= 1 .... ect will work, nor will it produce the desired array. (times=(0,3,5,6,7...)
So, this isnt going to work well.
My overall system will need to also know every event that has each time selected for a mass posting.
"Select * from table where time = $time (0-23) and day= $day (1-7)
Do action with data...
So with this requirement, I'm going to assume that storing the times as an array within the database is likely not the most efficient way either.
So am I stuck with needing up to 168 rows of data per event, or is there a better way I am missing? Thanks
Update:
To give a little more clarity on what I need to accomplish:
Users will be creating event campaigns in which other users can bid on various time slots for something to happen. There will likely be 10-100 thousand of these campaigns at any one time and they are ongoing until the creator stops them. The campaign creators can define the time slots available for their campaign.
At the designated time each day the system will find every campaign that has an event scheduled and perform the event.
So the first requirement is to know which time slots are available for the campaign, and then I need the system to quickly identify campaigns that have an event on each hour and day and perform it automatically.

Submitting Dates between Date Range - mySQL

In teaching myself php and mySQL a year or two ago, I created a movie database for the theater I work at, to help automate the more mundane web updates that need to be done weekly. So far this only pertains to film information (title, ratings, synopsis, start date) - but does not include showtimes, but lately I've thought it might be fun to see if it's possible to build on this aspect of the site.
My initial idea was to build a table that displayed showtimes via date groups (ie 5/15 - 5/20), but it was suggested that submitting times for individual days would probably be more useful from a user perspective (ie the showtimes for 5/15 are...), especially since there are occasionally changes and cancellations throughout the week due to special events.
Problem I'm having even starting this - is how to submit more than one date at once. Would it be possible, in the form, to have a 'start date' and 'end date' and have the single form submit all the times input on those two dates and all the dates inbetween?
From a back-end perspective submitting the same 4 or 5 showtimes every day, 7 days a week for 10 to 14 films a week is more work for US than just putting the times in manually in dreamweaver - so I'm just trying to figure out how to make the process easier for us behind the scenes. If we could batch insert a set of showtimes, then we could go back through and edit the individual days that have special/cancelled times (which would probably mean building in a 'draft, live, hidden' system - but I've got at least a basic knowledge of how to go about that.
I've got a very basic table set up for showtimes with (user end example of what I'm aiming for here):
MOVIE_ID (links with the movie info database to pull in film info - title, runtime, etc)
sched_date (uses datetime to set a specific showtime on a specific date)
I guess I might have to abandon using datetime if there's any way to do this?
Any help would be hot! Much thanks!
You can have 2 date fields with names from_date and end_date in your PHP form. In your PHP form submit code you then need to use the DateTime class and start from from_date and use the DateTime::add method to loop till end_date is reached. Please note you need to first convert the 2 dates i.e. from_date and end_date to datetime using either new DateTime('YYYY-MM-DD') or using DateTime::createFromFormat.
In the loop simply send insert commands to MySQL to insert the rows to your DB.

Is it good or bad practise to alter start dates in a database to the next occurrence of an event?

I am trying to create an event calendar which whilst initially quite small could turn out to be quite large. To that end, when trying to future proof it as much as possible, all events that occur in the past will be deleted from the database. However, is it bad practise to alter the start date of recurring events once they have happened to indicate when the next event will start? This makes it easier to perform search queries because theoretically no events will start more than say a week in the past, depending on how often the database is updated.
Is there a better way to do this?
My current intention is to have a table listing the event details along with a column for whether it is a yearly, monthly, weekly or daily recurrence. When somebody then searches for events between 2 dates, I simply look at each row and check if (EVENT START <= SEARCH FINISH && EVENT FINISH >= SEARCH START). This then gets all the possible events, and the recurring ones then need to be checked to see if they occur during the time period given. This is where I come a little unstuck, as to how to achieve this specifically. My thoughts are as follows:
Yearly: if EVENT START + 1 YEAR <= SEARCH FINISH || EVENT FINISH + 1 Year >= SEARCH START; repeat for +2 YEARS etc until EVENT START + NO YEARS > SEARCH FINISH.
Monthly: As above but + 1 month each time.
Weekly: As above but EVENT START and EVENT FINISH will be plus 7 DAYS BETWEEN RECURRENCE each iteration until EVENT START + 7 DAYS REPEATED > SEARCH FINISH.
Daily: As above but NO OF DAYS DIFFERENCE instead of 7 days for a week. This could be used to specify things like every 14 days (fortnight), every 10 days. Even every week could use this method.
However, when I think about the query that would have to be built to achieve this, I cannot help think that it will be very cumbersome and probably slow. Is there a better way to achieve the results I want? I have still not found a way to do things like occurs on the first Monday of a month or the last Friday of a month, or the second Saturday of April each year. Are these latter options even possible?
-- Edit: added below:
It might help a bit if I explain a bit more about what I am creating. That way guidance can be given with respect to that.
I am creating a website which allows organisations to add events, whether they are a one-off or recurring (daily, weekly, monthly, first Tuesday of a month etc.). The user of the site will then be able to search for events within a chosen distance (arbitrary 10, 25, 50, 100miles, all of country) on a set date or between 2 given dates which could be from 1 day apart up to a couple of years apart (obviously events that far into the future will be minimal or non-existant depending on the dates used).
The EVENTS table itself currently holds a lot of information about the event, such as location, cost, age group etc. Would it be better to have this in a separate table which is looked up once it has been determined if the event is within the specified search parameters? Clearly not all of this information is needed until the detailed page view, maybe just a name, location, cost and brief description.
I appreciate there are many ways to skin a cat but I am unsure how to skin this one. The biggest thing I am struggling with is how to structure my data so that a query will know if the recursion is within the specified date. Also, given that the mathematics to calculate distance between 2 lat/longs is relatively complex, I need to be able to build this calculation into my query, otherwise I will be doing the calculation in PHP anyway. Granted, there will be less results to process this way, but it still needs to be done.
Any further advice is greatly appreciated.
Creating events for each recurrence is unnecessary. It is much better to store the details that define how the event recurs. This question has been answered many times on SO.
One way to do this is to use a structure like this -
tblEvent
--------
id
name
description
date
tblEventRecurring
-----------------
event_id
date_part
end_date
Then you could use a query like this to retrieve events -
SELECT *
FROM `tblEvent`
LEFT JOIN `tblEventRecurring`
ON `tblEvent`.`id` = `tblEventRecurring`.`event_id`
WHERE (`tblEvent`.`date` = CURRENT_DATE AND `tblEventRecurring`.`event_id` IS NULL)
OR (
CURRENT_DATE BETWEEN `tblEvent`.`date` AND `tblEventRecurring`.`end_date`
AND (
(`tblEventRecurring`.`date_part` = 'D') OR
(`tblEventRecurring`.`date_part` = 'W' AND DAYOFWEEK(`tblEvent`.`date`) = DAYOFWEEK(CURRENT_DATE)) OR
(`tblEventRecurring`.`date_part` = 'M' AND DAYOFMONTH(`tblEvent`.`date`) = DAYOFMONTH(CURRENT_DATE))
)
)
UPDATE Added the following example of returning events for a given date range.
When returning dates for a given date range you can join the above query to a table representing the date range -
SET #start_date = '2012-03-26';
SET #end_date = '2012-04-01';
SELECT *
FROM (
SELECT #start_date + INTERVAL num DAY AS `date`
FROM dummy
WHERE num < (DATEDIFF(#end_date, #start_date) + 1)
) AS `date_list`
INNER JOIN (
SELECT `tblEvent`.`id`, `tblEvent`.`date`, `tblEvent`.`name`, `tblEventRecurring`.`date_part`, `tblEventRecurring`.`end_date`
FROM `tblEvent`
LEFT JOIN `tblEventRecurring`
ON `tblEvent`.`id` = `tblEventRecurring`.`event_id`
WHERE `tblEvent`.`date` BETWEEN #start_date AND #end_date
OR (`tblEvent`.`date` < #end_date AND `tblEventRecurring`.`end_date` > #start_date)
) AS `events`
ON `events`.`date` = `date_list`.`date`
OR (
`date_list`.`date` BETWEEN `events`.`date` AND `events`.`end_date`
AND (
(`events`.`date_part` = 'D') OR
(`events`.`date_part` = 'W' AND DAYOFWEEK(`events`.`date`) = DAYOFWEEK(`date_list`.`date`)) OR
(`events`.`date_part` = 'M' AND DAYOFMONTH(`events`.`date`) = DAYOFMONTH(`date_list`.`date`))
)
)
WHERE `date_list`.`date` BETWEEN #start_date AND #end_date
ORDER BY `date_list`.`date`;
You can replace the SQL variables with PHP vars if you would prefer. To display days without any events you can change the INNER JOIN between the two derived tables, date_list and events, to a LEFT JOIN.
The table dummy consists of a single column with numbers from 0 to whatever you anticipate needing. This example creates the dummy table with enough data to cover one month. You could easily populate it using an INSERT... SELECT... on the AI PK of another table -
CREATE TABLE `dummy` (
`num` SMALLINT UNSIGNED NOT NULL PRIMARY KEY
);
INSERT INTO `dummy` VALUES
(00), (01), (02), (03), (04), (05), (06), (07), (08), (09),
(10), (11), (12), (13), (14), (15), (16), (17), (18), (19),
(20), (21), (22), (23), (24), (25), (26), (27), (28), (29),
(30), (31);
Break it up
Have one table for vents that haven't happened yet with a reccurring event ID. So you can just poke one offs in there with recurring veent id of null. Get rid /archive past ones etc.
Have another for the data about recurring events.
When an event marked as recurring happens, go back to recurring table, check to see if it's enabled (you might want to add a range to them ie do this every wek for three months), and if all is okay, add a new record for the next time it occurs.
One way to do it anyway, and it gets rid of the problem of using event start for two different things which is why your code is getting complicated.
If you want future jobs from this. ie everything needed to do in the next month.
The it would be a union query. One to get all teh "current jobs", unioned with one to get all the jobs that will recur in the next month.
Can't stress this enough, get the data design right the code "just happens". If you data is messed up as in one field "start date" serving two different needs, then every time you go near it, you have to deal with that dual use. Forget it once and you get anything from a painful mess to a disaster.
Adding a Recurring_Start_Date column would be better than your current plan, wouldn't it. You wouldn't be asking this question, beacseu your data would fit your needs.
I assume you'll be searching through events much more frequently than you will be creating new ones. During event creation, I would create records for each occurrence of the event up to so reasonable amount of time (maybe for the next year or two).
It would also make things like "The third thursday of each month" a little easier. If you tried to do any of the calculations in a query it would be difficult and probably slow.

Best way to query calendar events?

I'm creating a calendar that displays a timetable of events for a month. Each day has several parameters that determine if more events can be scheduled for this day (how many staff are available, how many times are available etc).
My database is set up using three tables:
Regular Schedule - this is used to create an array for each day of the week that outlines how many staff are available, what hours they are available etc
Schedule Variations - If there are variations for a date, this overrides the information from the regular schedule array.
Events - Existing events, referenced by the date.
At this stage, the code loops through the days in the month and checks two to three things for each day.
Are there any variations in the schedule (public holiday, shorter hours etc)?
What hours/number of staff are available for this day?
(If staff are available) How many events have already been scheduled for this day?
Step 1 and step 3 require a database query - assuming 30 days a month, that's 60 queries per page view.
I'm worried about how this could scale, for a few users I don't imagine that it would be much of a problem, but if 20 people try and load the page at the same time, then it jumps to 1200 queries...
Any ideas or suggestions on how to do this more efficiently would be greatly appreciated!
Thanks!
I can't think of a good reason you'd need to limit each query to one day. Surely you can just select all the values between a pair of dates.
Similarly, you could use a join to get the number of events scheduled events for a given day.
Then do the loop (for each day) on the array returned by the database query.
Create a table:
t_month (day INT)
INSERT
INTO t_month
VALUES
(1),
(2),
...
(31)
Then query:
SELECT *
FROM t_month, t_schedule
WHERE schedule_date = '2009-03-01' + INTERVAL t_month.day DAY
AND schedule_date < '2009-03-01' + INTERVAL 1 MONTH
AND ...
Instead of 30 queries you get just one with a JOIN.
Other RDBMS's allow you to generate rowsets on the fly, but MySQL doesn't.
You, though, can replace t_month with ugly
SELECT 1 AS month_day
UNION ALL
SELECT 2
UNION ALL
...
SELECT 31
I faced the same sort of issue with http://rosterus.com and we just load most of the data into arrays at the top of the page, and then query the array for the relevant data. Pages loaded 10x faster after that.
So run one or two wide queries that gather all the data you need, choose appropriate keys and store each result into an array. Then access the array instead of the database. PHP is very flexible with array indexing, you can using all sorts of things as keys... or several indexes.

Categories