I am trying to set a start date and end date by the quarter.
For example, I am working on a reporting system where i need to report data for quarter 1, quarter 2, quarter 3, and quarter 4.
Quarter One - January - March
Quarter Two - April - June
Quarter Three - July - September
Quarter Four - October - December
I have for example some cases for the current month, and the previous month as shown below.
case 'this_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), date("d"), date("Y")));
break;
case 'last_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 0, date("Y")));
break;
But now i need to add cases for this and last quarter and I am not sure how to actually do that so it reflects the proper quarter range.
Any Ideas?
/**
* Compute the start and end date of some fixed o relative quarter in a specific year.
* @param mixed $quarter Integer from 1 to 4 or relative string value:
* 'this', 'current', 'previous', 'first' or 'last'.
* 'this' is equivalent to 'current'. Any other value
* will be ignored and instead current quarter will be used.
* Default value 'current'. Particulary, 'previous' value
* only make sense with current year so if you use it with
* other year like: get_dates_of_quarter('previous', 1990)
* the year will be ignored and instead the current year
* will be used.
* @param int $year Year of the quarter. Any wrong value will be ignored and
* instead the current year will be used.
* Default value null (current year).
* @param string $format String to format returned dates
* @return array Array with two elements (keys): start and end date.
*/
public static function get_dates_of_quarter($quarter = 'current', $year = null, $format = null)
{
if ( !is_int($year) ) {
$year = (new DateTime)->format('Y');
}
$current_quarter = ceil((new DateTime)->format('n') / 3);
switch ( strtolower($quarter) ) {
case 'this':
case 'current':
$quarter = ceil((new DateTime)->format('n') / 3);
break;
case 'previous':
$year = (new DateTime)->format('Y');
if ($current_quarter == 1) {
$quarter = 4;
$year--;
} else {
$quarter = $current_quarter - 1;
}
break;
case 'first':
$quarter = 1;
break;
case 'last':
$quarter = 4;
break;
default:
$quarter = (!is_int($quarter) || $quarter < 1 || $quarter > 4) ? $current_quarter : $quarter;
break;
}
if ( $quarter === 'this' ) {
$quarter = ceil((new DateTime)->format('n') / 3);
}
$start = new DateTime($year.'-'.(3*$quarter-2).'-1 00:00:00');
$end = new DateTime($year.'-'.(3*$quarter).'-'.($quarter == 1 || $quarter == 4 ? 31 : 30) .' 23:59:59');
return array(
'start' => $format ? $start->format($format) : $start,
'end' => $format ? $end->format($format) : $end,
);
}
I develop this function to deal with quarter in any way: relative (this, previous, first, last) and fixed.
Examples:
get_dates_of_quarter();
//return current quarter start and end dates
get_dates_of_quarter(2);
//return 2nd quarter start and end dates of current year
get_dates_of_quarter('first', 2010, 'Y-m-d');
//return start='2010-01-01' and end='2014-03-31'
get_dates_of_quarter('current', 2009, 'Y-m-d');
//Supposing today is '2014-08-22' (3rd quarter), this will return
//3rd quarter but of year 2009.
//return start='2009-07-01' and end='2009-09-30'
get_dates_of_quarter('previous');
//Supposing today is '2014-02-18' (1st quarter), this will return
//return start='2013-10-01' and end='2013-12-31'
Waiting this help someone ;)
Simple code:
$current_quarter = ceil(date('n') / 3);
$first_date = date('Y-m-d', strtotime(date('Y') . '-' . (($current_quarter * 3) - 2) . '-1'));
$last_date = date('Y-m-t', strtotime(date('Y') . '-' . (($current_quarter * 3)) . '-1'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With