get startdate and enddate for current quarter php

前端 未结 19 1621
悲哀的现实
悲哀的现实 2020-12-29 10:58

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

19条回答
  •  Happy的楠姐
    2020-12-29 11:24

    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.

提交回复
热议问题