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);