How to get first, second, third and fourth Monday of each month? - php

I'm working on an event based website. The user can create multiple events during the year and here is how the form works.
We have one select box with the following options :
The first
The second
The third
The fourth
And another select box with the names of the week days.
For example, he can choose to repeat his event every "second" "Monday" of each month during one year (or more).
Now, I'm looking for a way to get the date of each second Monday (as per the example above) with PHP.
I have this code which allows me to get what I want... But not sure about a great way to use it in a loop.
echo date("j, d-M-Y", strtotime("second monday 2017-11"));
Should I just loop over and change the year & month string?
Thanks!

Luckily, this problem has been solved already. The iCalendar standard has a Recurrence Rules component to deal with recurring events. There's an RFC specification, and there's an official PHP implementation as well. Alternatively, check out the tplaner/When library on GitHub.

Related

PHP - Getting the next occurrence of a date between a period

I'm working with icalendar events that make use of RRULE to deal with repetitions;
Now, i'm aware that the are some php class like When and RRules etc.. to handle RRULE and i'm already using it, to accomplish the task of generating repeating events, but the problem is about performance with long date-range.
So i thought to speed up the task of generating repetitions by limiting the range ( start & end ) by current calendar views that are [ MONTH, WEEK, DAY ].
Assuming we have a repeating event like
FREQ=DAILY;INTERVAL=1;DTSTART:2009-01-01
what i do is obviously to change the DTSTART to today date and to add an UNTIL date to limit the loop to a close range, and it is working just fine. the problem comes with rules like these:
FREQ=WEEKLY;BYDAY=SU;DTSTART:2009-01-01
or
FREQ=WEEKLY;INTERVAL=5;DTSTART:2009-01-01
with this kind of rules my trick doesn't work because the original start date doesn't match my harcoded today date.
i have tried without luck to make some iteration using DateTime Period and Interval but i can't figure it out.
So what i'm asking is a way to find when a given date will recur in my view range that can be MONTH, WEEK, or single DAY.
thanks in advance hope someone can help me. ;)
I know the question is quite old already, but I'm going to answer just in case.
There is no reliable way to alter the rule like you are trying to do. As you noticed, as soon as you start having anything more than just the basic daily/weekly/monthly/yearly repetition your trick doesn't work anymore. And you haven't even scratched the surface yet, things like BYSETPOS and COUNT are a real nightmare.
You only have two approaches:
Either loop through all occurrences, starting from DTSTART ignoring anything that is before the start of your period, and stopping once you reach the end of the period
Or generate the full list of dates of your period, and test each one against the RRULE. This can be done with a process of elimination (example: your RRULE only occurs on Sunday and the date is not a Sunday? Then discard and move on). However in the most complex cases, the only solution is to revert to option 1 and compute all the occurrences.
While you can code these yourself, I suggest to use a lib for that. I'm the author of php-rrule and with the lib you can use getOccurrencesBetween($begin, $end) (that implements option 1) and occursAt($date) (that implements option 2).

Can we simulate windows date and time to identify by php project?

I have a php project wherein there is option to select date for sending notifications on predefined interval starting from the selected date.
for eg. send notification to users on 5th of every month starting today.
My php script checks the system's/server's current date and time and run the scheduler accordingly to send the notifications.
I can test this by changing system's date and time but my question is i want to test whether the notifications are out on 5th date of every month without changing the system date and time.
I can't change the system's date and time due to some reasons. So can we simulate the future dates and let php identify that date as current date. If this is possible, please let me know how to achieve it.
Edit : one more thing i would like to add. I want to test this thing as a tester so i can't change the existing code.
Thanks & Regards
Zeeshan Shaikh
YOu can try http://php.net/manual/en/function.override-function.php to override time() function (or what you use to get the date and time).

Coding an interface where user selects date reoccurnace

I need to create an interface where a user needs to select how many times his magizine/newspaper gets distributed. Some magazines/newspapers get distributed
Several times a week.
Several times a a month: --either by day (e.g. every second tuesday) or by date (e.g. on the 1st, 10th and 28th)
Several times a year.
Once the frequency has been chosen, I need to add additional information to each frequency item. e.g. if the user chose weekly on Mondays and Fridays, I need to add additional information for Monday and separate information for Friday.
Does anyone have any ideas on how to do this? Or where I can read more or get some examples?
I'm using PHP and javascript/jquery for this application.
Thanks in advance.
You would need to ask the end user a series of questions, and use jquery to display to next set of questions for the next set of results etc

fullcalendar - recurring events

I as wondering if anyone could help me with a problem? I am using fullcalendar and it works great, but I would like to add the ability to repeat events upon user requests (by the way i'm using the selectable calendar). In other words I want to give them an option to recure the events based off whatever they choose (weekly, daily, monthly, unorganized days). I have read the docs and I know that recurring events are based off of the id and you basically add the days to the original date, but I am not sure how to do it without making a loop adding multiple events that may or may not be necessary.
My thought was to provide a column in mysql that gives a recurring option based off of the selection. Any ideas on how I should approach this. Does anyone possibly have an example that I can brainstorm on? All help is greatly appreciated. Thanks
Here's what I did to implement recurring events in fullcalendar.
http://fajitanachos.com/Fullcalendar-and-recurring-events/
I did end up using a loop to add events to my events table based on the recurring frequency. I know it's not the most efficient but it works for my purposes.
One thing to consider is how much flexibility do you need? Do you need an event to repeat every Tuesday/Thursday, every 3rd Saturday, the last day of the month, or simply every week on a certain day? The more flexibility you need, the more complex your solution will need to be.
It's nothing to do with fullcalendar. You need to have this implemented in your back-end. I wouldn't recommend you creating a column in your events table in order to maintain recurring once you might need a recurring type (weekly, daily, monthly, yearly) or frequency for example.
I've faced this same type of solution few months ago and I would recommend you create another table called tb_events_recurring, for example, saving information like event_id, date_start, date_end and recurring type if necessary.
Create an event for each recurring are hard stuff to maintain. My advise is creating this events every time fullcalendar request them. You have the date_start and date_end in your post vars, so it won't be cost for your server. Put all events in an array and after encode 'em to json.

Building a one week calendar / schedule

My website involves scheduling recurring lessons and events. At present I have built a table using php and javascript which scans through my mysql DB, time slot by timeslot, day by day to see if there is an event scheduled or if the timeslot is free.
It works fine, but is quite clunky and ugly and to be honest, not user friendly enough. I have now built the rest of the site using JQuery along with php, JS, HTML.
I have searched far and wide for a JQuery module to sort me out for this, but every calendar/scheduler I find is for ongoing time, i.e. like the Outlook Calendar.
I want one that ONLY displays Monday - Sunday, with time intervals of a half hour - what's the best way for me to achieve this?
Many thanks in advance,
Brett
may be this can be of help if you haven checked it out already https://github.com/robmonie/jquery-week-calendar/wiki/

Categories