PHP Monthly Recurrence - php

I'm Using PHP RRule for Recurrence in my Project.
Here Daily and Weekly Recurrence are working fine.
I don't know how to use monthly Recurrence in my scenario.
Monthly scenario:
First Sunday of every 2 Months
Code:
$dow=array("SU");
$rrule = new RRule([
'FREQ' => 'MONTHLY',
'BYMONTHDAY' => 2,
'INTERVAL' => 2,
'BYDAY' => $dow,
'DTSTART' => '2016-12-01',
'UNTIL' => '2017-12-31'
]);
foreach ( $rrule as $occurrence ) {
echo "</br>";
echo $occurrence->format('D Y-m-d');
}
echo $rrule->humanReadable(),"\n";
//OUTPUT
Sun 2017-04-02
every 2 months on Sunday on the 2nd of the month, starting from 12/1/16, until 12/31/17

You don't need BYMONTHDAY, you need BYDAY with a number prefix.
In your situation: 'BYDAY' => '1SU' means "the first Sunday". 2SU the second Sunday, -1SU the last Sunday etc.
More information:
https://github.com/rlanvin/php-rrule/issues/19
https://www.rfc-editor.org/rfc/rfc5545#section-3.3.10

Related

Get Start and end date of Week according to string

I want to display the start date and end date of the week. I have One date and a string like 1W4 and,in 1W4 consider 4 weeks and 1 visit so, my string like this 2W4,1W2,3W3,1W1,2W4.
I want to make start date and end date of week array according to string and week start from Sunday to Saturday.
Please post me if anyone has solution.Please ignoring if mistake in asking Question.
Thank you.
Try my php code:
From php.net datetime.format:
W: ISO-8601 week number of year, weeks starting on Monday.
The first calendar week of a year is that one which includes the first Thursday of that year.
So I have to rest one day to the start week date.
I assumed that the weeks correspond to the current year.
Input string:
$weeksString = "2W4,1W2,3W3,1W1,2W4";
Code:
<?php
$weeksArray = explode(",", $weeksString);
$result = array();
foreach($weeksArray as $visitsWeek) {
list($visits, $week) = explode("W", $visitsWeek);
$startDate = date("Y-m-d", strtotime(date("Y") . "W" . str_pad($week, 2, "0", STR_PAD_LEFT) . " -1 days"));
$endDate = date("Y-m-d", strtotime($startDate . " +6 days"));
$result[] = array("week" => $week, "startDate" => $startDate, "endDate" => $endDate);
}
?>
Output array $result:
array ( 0 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), 1 => array ( 'week' => '2', 'startDate' => '2021-01-10', 'endDate' => '2021-01-16', ), 2 => array ( 'week' => '3', 'startDate' => '2021-01-17', 'endDate' => '2021-01-23', ), 3 => array ( 'week' => '1', 'startDate' => '2021-01-03', 'endDate' => '2021-01-09', ), 4 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), )

Laravel calendar scheduler

I'm trying to make an calendar scheduler for student teacher for their future lessons together.
I'm trying to add day, time and DATE(most important). Right now I added day and time in each of their following meetings. For ex. every Monday at 08:00 and every Wednesday at 10:00. But what I cannot add is Date . Here is some of my coding.
$student = Auth::user()->student;
$teacher = Teacher::where('id',$id)->first();
$lessons = $request->lessons_booked;
$times = $request->times;
$timesArrLength = count($times);
$stuTeachArr = array();
for($i = 1;$i<= $lessons;$i++){
$studentTeacher = StudentTeacher::create([
'student_id' => $student->id,
'teacher_id' => $teacher->id,
'subject_id' => $request->subject_id,
'place_of_tutoring_id' => $request->place_of_tutoring_id,
'lessons_booked' => $lessons,
'lesson_nr' => $i,
'time_id' => $times[($i-1) % $timesArrLength]['id'],
'day' => $times[($i-1) % $timesArrLength]['day'],
'date' => '2021-01-01', // add date here
'lessons_left' => $request->lessons_booked - $i,
'status' => 'active'
]);
array_push($stuTeachArr,$studentTeacher);
}
// $stuTeachArr['booked_teacher'] = $studentTeacher;
return $stuTeachArr;
What I want is to add DATE for every lesson that will happen from now until they finish with their lessons(For ex. every Monday and every Wednesday as mentioned before).
Any suggestion to change the way I'm doing would be welcomed.

If date string found to be between dates, return pay_period_start & pay_period_end. How?

The following code works if I manually type in every day for every single month in each hard coded array.
I then loop through the arrays for a match and if I find it, I return the first index and the last index value of that array. These are the pay period start and end dates later to be used with mysql select queries.
// MOUNTAIN DAYLIGHT SAVINGS TIME
date_default_timezone_set('MST7MDT');
// = PHP Default TimeZone
//print 'MOUNTAIN DAYLIGHT SAVINGS TIME';
//print '<p>';
// = MySQL CURRDATE() in MySQL DATETIME Format.
$php_current_date = date('Y-m-d');
// 2019 Pay Periods - MONTHLY
$parent_array = array(
1 => array('2019-01-01','2019-01-31'),
2 => array('2019-02-01','2019-02-28'),
3 => array('2019-03-01','2019-03-31'),
4 => array('2019-04-01','2019-04-30'),
5 => array('2019-05-01','2019-05-31'),
6 => array('2019-06-01','2019-06-30'),
7 => array('2019-07-01','2019-07-31'),
8 => array('2019-08-01','2019-08-31'),
9 => array('2019-09-01','2019-09-30'),
10 => array('2019-10-01','2019-10-31'),
11 => array('2019-11-01','2019-11-30'),
12 => array('2019-12-01','2019-12-31'),
13 => array('2020-01-01','2020-01-31'),
14 => array('2020-02-01','2020-02-29'),
15 => array('2020-03-01','2020-03-31'),
16 => array('2020-04-01','2020-04-30'),
17 => array('2020-05-01','2020-05-31'),
18 => array('2020-06-01','2020-06-30'),
19 => array('2020-07-01','2020-07-31'),
20 => array('2020-08-01','2020-08-31'),
21 => array('2020-09-01','2020-09-30'),
22 => array('2020-10-01','2020-10-31'),
23 => array('2020-11-01','2020-11-30'),
24 => array('2020-12-01','2020-12-31')
);
$current_pay_period_start = '';
$current_pay_period_end = '';
// For each child Array of date Strings inside parent Array of arrays...
foreach($parent_array as $child_array){
// Speculate the variable name as $result_found while searching each child Array of date Strings
// for the Current date in *Mountain Daylight Savings Time
$result_found = in_array($php_current_date, $child_array);
// if we have a match...
if ($result_found) {
// GET LEFT-MOST index and assign it to a variable.
$current_pay_period_start = current($child_array);
// GET RIGHT-MOST index and assign it to another variable.
$current_pay_period_end = end($child_array);
// Add a day for mysql query logic...
// because mysql uses < instead of =< for comparison in the query the follows...
$current_pay_period_end = date('Y-m-d', strtotime($current_pay_period_end . ' + 1 days'));
/*
Following Works ONLY on direct access.
Debug Only.
Eg. localhost/folder/filename.php
*/
print 'Php Current Date: ' . $php_current_date;
print '<br>';
print 'Current Pay Period Start: ' . $current_pay_period_start;
print '<br>';
print 'Current Pay Period End: ' . $current_pay_period_end;
exit;
}
}
I have tried to implement the solution below but, I keep getting errors related to me not being able to compare date strings... It seems I have learned to find date strings in an array of arrays but they aren't really dates as far as php is concerned.
/**
* #param DateTime $date Date that is to be checked if it falls between $startDate and $endDate
* #param DateTime $startDate Date should be after this date to return true
* #param DateTime $endDate Date should be before this date to return true
* return bool
*/
function isDateBetweenDates(DateTime $date, DateTime $startDate, DateTime $endDate) {
return $date > $startDate && $date < $endDate;
}
$fromUser = new DateTime("2012-03-01");
$startDate = new DateTime("2012-02-01 00:00:00");
$endDate = new DateTime("2012-04-30 23:59:59");
echo isDateBetweenDates($fromUser, $startDate, $endDate);
Here's how I try to call it and get the error...
isDateBetweenDates($php_current_date, $current_pay_period_start, $current_pay_period_end);
I have wrote the following for you, that compares the two dates from your array. Hopefully it will help!
$php_current_date = date('Y-m-d');
$parent_array = array(
1 => array('2019-01-01','2019-01-31'),
2 => array('2019-02-01','2019-02-28'),
3 => array('2019-03-01','2019-03-31'),
4 => array('2019-04-01','2019-04-30'),
5 => array('2019-05-01','2019-05-31'),
6 => array('2019-06-01','2019-06-30'),
7 => array('2019-07-01','2019-07-31'),
8 => array('2019-08-01','2019-08-31'),
9 => array('2019-09-01','2019-09-30'),
10 => array('2019-10-01','2019-10-31'),
11 => array('2019-11-01','2019-11-30'),
12 => array('2019-12-01','2019-12-31'),
13 => array('2020-01-01','2020-01-31'),
14 => array('2020-02-01','2020-02-29'),
15 => array('2020-03-01','2020-03-31'),
16 => array('2020-04-01','2020-04-30'),
17 => array('2020-05-01','2020-05-31'),
18 => array('2020-06-01','2020-06-30'),
19 => array('2020-07-01','2020-07-31'),
20 => array('2020-08-01','2020-08-31'),
21 => array('2020-09-01','2020-09-30'),
22 => array('2020-10-01','2020-10-31'),
23 => array('2020-11-01','2020-11-30'),
24 => array('2020-12-01','2020-12-31')
);
foreach ($parent_array as $child_array) {
//compare dates using strtotime, did the conversion in the if statement to retain the original date format, for output if results are found.
if (strtotime($php_current_date) >= strtotime($child_array[0]) && strtotime($php_current_date) <= strtotime($child_array[1])) {
// match found...
$current_pay_period_start = $child_array[0];
$current_pay_period_end = $child_array[1];
print 'Php Current Date: ' . $php_current_date;
print '<br>';
print 'Current Pay Period Start: ' . $current_pay_period_start;
print '<br>';
print 'Current Pay Period End: ' . $current_pay_period_end;
exit;
}
}
I have tested it and the following is outputted:
Php Current Date: 2019-03-07
Current Pay Period Start: 2019-03-01
Current Pay Period End: 2019-03-31
Hopefully this will help!

Grouping data to get reports by intervals

I posted a question some time ago on representing data per date horizontally on a datatable.
See here: datatables dates at the top and data cells going from left to right
With the help of that thread I was able to get the data to display how I wanted it. With the dates showing at the top, the service provided on the left and all data associated with any date between the 2 date paramters inside the main body. (If there is no data in a particular date then the < td > will display 0. See here:
http://www.phpwin.org/s/ewbAS6
After manipulating this code further I made the dates of the search dynamic by proving a form with a start date and an end date, and a dropdown with the options of:
Daily
Weekly
Monthly
Quaterly
Yearly
this allows the interval of dates at the top to become dynamic. Of course all this is doing is changing the value of the 2nd parameter inside the date while loop.
WHILE (strtotime($date) <= strtotime($end_date)) {
echo '<th>' . $date . '</th>';
$date = date('Y-m-d', strtotime($date . ' +1day'));
}
with the parameter set at Weekly, the value of +1day becomes +1week, at Monthly; the value becomes +1month and so on.
MY ISSUE:
When the interval is set to daily, the dates with their corresponding attendance counts are displayed correctly but once you try to increase the interval to +1week and above the data does not round up to the week shown. Check this:
[LINK1]Per day: http://www.phpwin.org/s/ewbAS6
[LINK2]Per month: http://www.phpwin.org/s/xRo3I6
Looking at the array (modified on the LINK2)
$result[] = array('Service Name' => 'Health', 'date' => '2017-04-04', 'Attendance' => 5);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-04-16', 'Attendance' => 5);
$result[] = array('Service Name' => 'Saturday Youth Meeting', 'date' => '2017-04-03', 'Attendance' => 1);
$result[] = array('Service Name' => 'Saturday Youth Meeting', 'date' => '2017-05-03', 'Attendance' => 3);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-05-03', 'Attendance' => 2);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-04-11', 'Attendance' => 3);
$result[] = array('Service Name' => 'Payroll', 'date' => '2018-04-03', 'Attendance' => 10);
You can see in the array that there are multiple attendance entries in April, totaling 14 Attendances in that month however during LINK2 where the interval is increased to a month instead of showing 14 for April (which would be the sum of all the dates in that particular month) it shows the value 1.
My live version takes the array from a database so I used the YEAR(), MONTH(), WEEK() and DAY() function on the date and used group by. The query executes how I want it but having issues working on the PHP end.

Dynamic date definition and conditions

I am building a small class combination to calculate the precise date of the beginning of a semester. The rules for determining the beginning of the semester goes as follow :
The monday of week number ## and after dd-mm-yyyy date
ie: for winter its week number 2 and it must be after the january 8th of that year
I am building a resource class that contain these data for all the semesters (4 in total). But now I am facing an issue based on the public holidays. Since some of those might be on a Monday, in those cases I need to get the date of the Tuesday.
The issue I am currently working on is the following :
The target semester begins on or after august 30 and must be on week 35.
I also have to take account of a public holiday which happen on the first monday of september.
The condition in PHP terms is the following
if (date('m', myDate) == 9 // if the month is september
&& date('w', myDate) == 1 // if the day of the week is monday
&& date('d', myDate) < 7 // if we are in the first 7 days of september
)
What would be the best way to "word" this as a condition and store it in an array?
EDIT
I might not have been clear enough, finding the date is not the problem here. The actual problem is storing a condition in a configuration array that looks like the following :
$_ressources = array(
1 => array(
'dateMin' => '08-01-%',
'weekNumber' => 2,
'name' => 'Winter',
'conditions' => array()
),
2 => array(
'dateMin' => '30-04-%',
'weekNumber' => 18,
'name' => 'Spring',
'conditions' => array()
),
3 => array(
'dateMin' => '02-07-%',
'weekNumber' => 27,
'name' => 'Summer',
'conditions' => array()
),
4 => array(
'dateMin' => '30-08-%',
'weekNumber' => 35,
'name' => 'Autumn',
'conditions' => array("date('m', %date%) == 9 && date('w', %date%) == 1 && date('d', %date%) < 7")
)
);
The issue I have with the way it's presented now, is that I will have to use the eval() function, which I would rather not to.
You said:
The target semester begins on or after august 30 and must be on week 35.
If that's the case you can simple check for week number.
if(date('W', myDate) == 35)
Or if your testing condition is correct then you should compare day number till 7 as it starts from 1.
if((date('m', myDate) == 9 // september
&& date('w', myDate) == 1 // monday
&& date('d', myDate) <= 7 // first 7 days of september
)
And then in the if statement, once you have found the monday which would be OK IF its not a public holiday, do this
if(...){
while(!array_search (myDate, aray_of_public_holidays))
date_add($myDate, date_interval_create_from_date_string('1 days'));
}
Here the array_of_public_holidays contains the list of public holidays.
Update with Code
Following code should work for your purposes
<?php
// array with public holidays
$public_holidays = array(/* public holidays */);
// start on 30th august
$myDate = new DateTime('August 30');
// loop till week number does not cross 35
while($myDate->format('W') <= 35){
// if its a monday
if($myDate->format('w') == 1){
// find the next date not a public holiday
while(array_search($myDate, $public_holidays))
$myDate->add(date_interval_create_from_date_string('1 days'));
// now myDate stores the valid semester start date so exit loop
break;
}
// next date
$myDate->add(date_interval_create_from_date_string('1 days'));
}
// now myDate is the semester start date
?>
Update according to updated question
Following code should work for your needs. You do not need to store the condition in your array as PHP code. The following code shows how it can be done
// semester conditions
$sem_conditions = array(
1 => array(
'dateMin' => '08-01-%',
'weekNumber' => 2,
'name' => 'Winter'
),
2 => array(
'dateMin' => '30-04-%',
'weekNumber' => 18,
'name' => 'Spring'
),
3 => array(
'dateMin' => '02-07-%',
'weekNumber' => 27,
'name' => 'Summer'
),
4 => array(
'dateMin' => '30-08-%',
'weekNumber' => 35,
'name' => 'Autumn'
)
);
// array with public holidays format (d-M)
$public_holidays = array('05-09', '10-01');
// store sem starts
$sem_starts = array();
// for each semester
foreach($sem_conditions as $sem){
// start date
$myDate = date_create_from_format('d-m', substr($sem['dateMin'], 0, -2));
// loop till week number does not cross $sem['weekNumber']
while($myDate->format('W') <= $sem['weekNumber']){
// if its a monday
if($myDate->format('w') == 1){
// find the next date not a public holiday
while(array_search($myDate->format('d-m'), $public_holidays) !== false)
$myDate->add(date_interval_create_from_date_string('1 days'));
// now myDate stores the valid semester start date so exit loop
break;
}
// next date
$myDate->add(date_interval_create_from_date_string('1 days'));
}
// add to sem starts
$sem_start[$sem['name']] = $myDate->format('d-m-Y');
}
var_dump($sem_start);
The target semester begins on or after august 30 and must be on week 35
The start of the semester is the minimal date between week 35 and August 30:
$week35 = new DateTime("January 1 + 35 weeks");
$august30 = new DateTime("August 30");
$start = min($week35, $august30);
Alternatively:
$start = min(date_create("January 1 + 52 weeks"), date_create("August 30"));

Categories