2

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.
  • 3
    Have you tried anything? Looked at what MySQL and PHP date/time functions might aid in this? For example [finding if a date is a weekend](http://stackoverflow.com/questions/4802335/checking-if-date-is-weekend-php)... – ficuscr Aug 10 '15 at 19:19
  • 1
    What *have you tried*? – Rob W Aug 10 '15 at 19:19
  • Sorry! Added in what I have at the moment to the question. – Russell Bishop Aug 10 '15 at 19:30
  • @RussellBishop are you trying to do something [like this](http://codepad.viper-7.com/ReQZTq)? – billyonecan Aug 10 '15 at 20:06
  • @billyonecan You have just filled in most of the puzzle! One thing I'd like to know is how to use this kind of array generation only between the first and last dates of my records, so if they spanned 2 years and three months that's how many arrays it would produce. However - if that's the wrong way of approaching this, please point me in the right direction. Thanks so much! – Russell Bishop Aug 10 '15 at 20:36
  • Just get the dates of the earliest and latest records as `Ym` and use `range($minDate, $maxDate)` – billyonecan Aug 11 '15 at 07:03
  • Thanks so much - I got it to work using: `// first and last record's months $firstrecord = $database->min("records", "datetime"); $lastrecord = $database->max("records", "datetime"); // convert to 201501 to 201512 $fm = date("Ym", strtotime($firstrecord)); $lm = date("Ym", strtotime($lastrecord)); // create months foreach (range($fm, $lm) as $i => $monthNum) {` – Russell Bishop Aug 19 '15 at 22:09
  • If you want to add that as an answer I'll mark it as solved. – Russell Bishop Aug 19 '15 at 22:10

0 Answers0