Jump to content

fiscal month


jakebur01

Recommended Posts

I need to find the last Friday of each month and determine the start and end dates of each month based on the last Friday.  So, for September, the last Friday was the 27th, that means Saturday the 28th will be the last day of September.  September 29 will then start the first day of October....  October 25 is the last Friday in October, so Saturday October 26 will close out October.....  Sun Oct 27 starts November, etc...

7/28 - 8/31

9/1 - 9/28

9/29 - 10/26

10/27 - 11/30

I can build a table and manually key in the start and end dates then query as needed, but if there is a way to do it on the fly, I'd rather do it in php to eliminate mistakes.

 

Below is the code I'm currently using to find the first and last day of each calendar month, but I'll need to convert this to find the fiscal month based on the last Saturday of each month.  And go back a few years in the option select.

      <select name="dateselect" id="dateselect">


          <?php


          $today = date("Y-m-d");
          $monthbegin =   date("Y-m-d",strtotime('first day of this month', strtotime($today)));

          for( $i= 0 ; $i <= 52 ; $i++ )
          {
          if($i == 0){
          $monthselect = date('Y-m-d',strtotime($monthbegin));
          $monthdisplay = date('F Y',strtotime($monthbegin));
          echo '<option ' . ($i == 0 ? 'selected=\'selected\'' : '') . ' value="' . $monthselect . '" >' . $monthdisplay . '</option>';
          }
          else{
          $monthselect = date('Y-m-d',strtotime('-'."$i".' months',strtotime($monthbegin)));
          $monthdisplay = date('F Y',strtotime('-'."$i".' months',strtotime($monthbegin)));
          echo '<option ' . ($i == 0 ? 'selected=\'selected\'' : '') . ' value="' . $monthselect . '" >' . $monthdisplay . '</option>';

          }




          }
          ?>
          </select>
  $begin_date = $_GET["dateselect"];
  $end_date = date("Y-m-t", strtotime($begin_date));

 

Edited by jakebur01
Link to comment
Share on other sites

the DateTime class can handle a lot of default scenarious, including last x day of y, so all you need is the year and month component

<?php

$d = new DateTime('2019-10-01');
$d->modify('last friday');
print_r($d); // 2019-09-27

$d = new DateTime('2019-11-01');
$d->modify('last friday');
print_r($d); // 2019-09-25

$d = new DateTime('2019-12-01');
$d->modify('last friday');
print_r($d); // 2019-09-29

 

Link to comment
Share on other sites

You can generate a date preiod object to loop through a range of months

<?php
// CREATE A RANGE OF MONTHS
$dt1 = new DateTime('2019-01-01');
$dt2 = new DateTime('2020-01-01');
$months = [];
$dp = new DatePeriod($dt1, new DateInterval('P1M'), $dt2);

// STARTING FRIDAY
$prev_fri = new DateTime('last friday of december 2018');

// LOOP THROUGH THE MONTHS
foreach ($dp as $d) {
    $key = $d->format('Y-m');
    $months[$key]['start'] = $prev_fri->modify('+1 days')->format('Y-m-d');    // add 1 day to previous friday for start date
    $fri = $d->modify('last friday of this month');                            // get last friday of the month for end date
    $months[$key]['end'] = $fri->format('Y-m-d');
    $prev_fri = $fri;                                                          // store month end
}

echo '<pre>', print_r($months, 1), '</pre>';
?>

Giving

Array
(
    [2019-01] => Array
        (
            [start] => 2018-12-29
            [end] => 2019-01-25
        )

    [2019-02] => Array
        (
            [start] => 2019-01-26
            [end] => 2019-02-22
        )

    [2019-03] => Array
        (
            [start] => 2019-02-23
            [end] => 2019-03-29
        )

    [2019-04] => Array
        (
            [start] => 2019-03-30
            [end] => 2019-04-26
        )

    [2019-05] => Array
        (
            [start] => 2019-04-27
            [end] => 2019-05-31
        )

    [2019-06] => Array
        (
            [start] => 2019-06-01
            [end] => 2019-06-28
        )

    [2019-07] => Array
        (
            [start] => 2019-06-29
            [end] => 2019-07-26
        )

    [2019-08] => Array
        (
            [start] => 2019-07-27
            [end] => 2019-08-30
        )

    [2019-09] => Array
        (
            [start] => 2019-08-31
            [end] => 2019-09-27
        )

    [2019-10] => Array
        (
            [start] => 2019-09-28
            [end] => 2019-10-25
        )

    [2019-11] => Array
        (
            [start] => 2019-10-26
            [end] => 2019-11-29
        )

    [2019-12] => Array
        (
            [start] => 2019-11-30
            [end] => 2019-12-27
        )

)

 

Link to comment
Share on other sites

I don't know how to do exactly yet, but this is what I have in mind.  Is this the right direction?

$today =   date("Y-m-d");
$originalstart =   date("Y-m-d",strtotime('-24 months', strtotime($originalstart)));

for( $i= 1 ; $i <= 24 ; $i++ )
{

  $start =   date('Y-m-d',strtotime('+'."$i".' month', strtotime($originalstart)));
  //need to make day 01 somehow

  $firstday = new DateTime($start);
  $firstday->modify('first day of this month');
if(sunday){
  //use sunday
}
elseif(tuesday thru thursday){
  //use this past sunday
}
elseif(friday or saturday){
  //use next Sunday
}

$lastday = new DateTime($start);
$lastday->modify('+1 month');
$lastday->modify('last friday');
$lastday->modify('+1 day');


}

 

Link to comment
Share on other sites

If you want always to go back 2 years, then a slight modification to my above code

<?php
// CREATE A RANGE OF MONTHS  //
    $dt1 = new DateTime('-2 years');
    $dt2 = new DateTime();
    $dp = new DatePeriod($dt1, new DateInterval('P1M'), $dt2);

// STARTING FRIDAY  //
    $fri = clone $dt1;
    $fri->modify('last friday of previous month');

// LOOP THROUGH THE MONTHS    //
// AND STORE IN months ARRAY  //
    $months = [];
    foreach ($dp as $d) {
        $key = $d->format('Y-m');
        $months[$key]['start'] = $fri->modify('+1 days')->format('Y-m-d');    // add 1 day to previous friday for start date
        $fri = $d->modify('last friday of this month');                       // get last friday of the month for end date
        $months[$key]['end'] = $fri->format('Y-m-d');
    }

// CHECK THE RESULTS  //
    echo '<pre>';
    printf("<b>%-14s%s</b>\n", 'Month', 'Fiscal month');
    printf("<b>%-10s%10s &mdash; %-10s</b>\n", '', 'Saturday', 'Friday');
    print("--------+------------------------\n");
    foreach ($months as $k => $m) {
        printf("%-7s | %-10s &mdash; %-10s\n", $k, $m['start'], $m['end']);
    }
    print("--------+------------------------\n");
    echo '</pre>';
?>

Giving

Month         Fiscal month
            Saturday — Friday    
--------+------------------------
2017-11 | 2017-10-28 — 2017-11-24
2017-12 | 2017-11-25 — 2017-12-29
2018-01 | 2017-12-30 — 2018-01-26
2018-02 | 2018-01-27 — 2018-02-23
2018-03 | 2018-02-24 — 2018-03-30
2018-04 | 2018-03-31 — 2018-04-27
2018-05 | 2018-04-28 — 2018-05-25
2018-06 | 2018-05-26 — 2018-06-29
2018-07 | 2018-06-30 — 2018-07-27
2018-08 | 2018-07-28 — 2018-08-31
2018-09 | 2018-09-01 — 2018-09-28
2018-10 | 2018-09-29 — 2018-10-26
2018-11 | 2018-10-27 — 2018-11-30
2018-12 | 2018-12-01 — 2018-12-28
2019-01 | 2018-12-29 — 2019-01-25
2019-02 | 2019-01-26 — 2019-02-22
2019-03 | 2019-02-23 — 2019-03-29
2019-04 | 2019-03-30 — 2019-04-26
2019-05 | 2019-04-27 — 2019-05-31
2019-06 | 2019-06-01 — 2019-06-28
2019-07 | 2019-06-29 — 2019-07-26
2019-08 | 2019-07-27 — 2019-08-30
2019-09 | 2019-08-31 — 2019-09-27
2019-10 | 2019-09-28 — 2019-10-25
--------+------------------------

 

Link to comment
Share on other sites

I believe I may have it.

$originalstart =   date("Y-m-d");
$originalstart =   date("Y-m-d",strtotime('-24 months', strtotime($originalstart)));

for( $i= 0 ; $i <= 25 ; $i++ )
{

  $start =   date('Y-m-01',strtotime('+'."$i".' month', strtotime($originalstart)));

$firstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month', strtotime($start)))));
$lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month', strtotime($start)))));

echo "$firstday - $lastday<br />";

}

2017-10-01 - 2017-10-28
2017-10-29 - 2017-11-25
2017-11-26 - 2017-12-30
2017-12-31 - 2018-01-27
2018-01-28 - 2018-02-24
2018-02-25 - 2018-03-31
2018-04-01 - 2018-04-28
2018-04-29 - 2018-05-26
2018-05-27 - 2018-06-30
2018-07-01 - 2018-07-28
2018-07-29 - 2018-09-01
2018-09-02 - 2018-09-29
2018-09-30 - 2018-10-27
2018-10-28 - 2018-12-01
2018-12-02 - 2018-12-29
2018-12-30 - 2019-01-26
2019-01-27 - 2019-02-23
2019-02-24 - 2019-03-30
2019-03-31 - 2019-04-27
2019-04-28 - 2019-06-01
2019-06-02 - 2019-06-29
2019-06-30 - 2019-07-27
2019-07-28 - 2019-08-31
2019-09-01 - 2019-09-28
2019-09-29 - 2019-10-26
2019-10-27 - 2019-11-30

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.