get startdate and enddate for current quarter php
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'));
what about:
$offset = (date('n')%3)-1; // modulo ftw
$start = new DateTime("first day of -$offset month midnight");
$offset = 3-(date('n')%3); // modulo ftw again
$end = new DateTime("last day of +$offset month midnight");
/**
* 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 ;)