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, qu
A versatile version would be:
$date = new DateTime(/* you may insert a date here, else its now */);
//$date->modify('-3 months'); // you may want last quarter or any other modifcation
$quarter = ceil($date->format('n')/3);
$start = new DateTime();
$start->setDate($date->format('Y'), (($quarter*3)-2), 1)->setTime(0, 0, 0, 0);
$end = new DateTime();
$end->setDate($date->format('Y'), ($quarter*3), 1);
$end->setDate($date->format('Y'), ($quarter*3), $end->format('t'))->setTime(0, 0, 0, 0);
echo $start->format('Y-m-d');
echo $end->format('Y-m-d');
this is simple yet effective. It let's you choose the input date and make relative adaptions with modify.