I have an array of records, each with a datetime (2015-01-01 00:00:00). I would like to be able to split this array into records that are between paydates.
The example paydate I'll use is the 25th e.g. the start of a month would be "last weekday on or before the 25th of the month" - meaning if the 25th of a month is a weekend, we'll want that month to start on the previous weekday (meaning Friday).
Eventually I'd like to be able to sort the records like so:
July 2015 [June 25th - July 23rd]
- Record 1 - 2015-06-27 00:00:00
- Record 2 - 2015 07-15 00:00:00
August 2015 [July 24th - August 24th]
- Record 1 - 2015-07-25 00:00:00
- Record 2 - 2015 08-09 00:00:00
So far I have built this array (uses Medoo framework to handle the records):
$paydate = '25th';
// Testing July Dates
$monthNum = 6;
$monthName = date('F', mktime(0, 0, 0, $monthNum, 10));
$nextMonthName = date('F', mktime(0, 0, 0, $monthNum+1, 10));
$dates = array(
array(...),
array(
"name" => "July",
"start" => date('Y-m-d 00:00:00', strtotime('last weekday ' . $paydate . $monthName . ' 2015')),
"end" => date('Y-m-d H:i:s', strtotime('last weekday ' . $paydate . $nextMonthName . ' 2015') - 1),
),
array(...),
);
Followed by:
foreach ($dates as $date) {
// get this month's records
$thismonthsrecords = $database->select('records',
[...],
[...],
[
'datetime[<>]' => [$date['start'], $date['end']]
]
);
Problems at the moment:
- Only works manually (obviously).
- Last weekday X will get me the last weekday even if that date is already the last weekday, so giving it a Tuesday 25th will turn it to a Monday 24th.