1 (edited by p2409 05/29/2011 03:43:33 pm)

Topic: Function to get dates of quarters

Hi Guys

Mucking about modules at the moment, and have just written a versatile function to calculate start/end dates of quarters. With some adjustment to fit naming standards etc. it might be useful to include in date_functions.inc at some point. Hope someone finds it useful!
Pete

/**
 * Get last quarter start and end dates for today. Eg. 13/12/2010 = 1/10/2009, 31/12/2009
 * @param array $qtr_dates Referenced array of quarter dates (0=start, 1=end)
 * @param int $num_qtrs How many quarters back/forward (default 0 = this quarter)
 */
function get_quarter_dates(&$qtr_dates, $num_qtrs = 0) {
    // work out the last quarter dates, and store in the qtr_dates array
    include_once($path_to_root . "/includes/date_functions.inc");
    // 1. Get current month (number)
    $this_month = date('m');
    //$this_month = 1; TEST
    // 2. Calculate the start month for this quarter.
    $this_qtr = intval(($this_month - 1)/3) * 3 + 1;
    // 3. Subtract 3 months from this quarter start date to get last quarter
    $qtr_dates[0] = add_months(__date(date('Y'),$this_qtr,01),$num_qtrs*3);
    // 4. Add two months and get last day of that month for end of last quarter.
    $qtr_dates[1] = end_month((add_months($qtr_dates[0],2)));
}