get startdate and enddate for current quarter php

前端 未结 19 1629
悲哀的现实
悲哀的现实 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条回答
  •  情歌与酒
    2020-12-29 11:25

    Try to use DateTime function. For your example, is look like:

    case 'this_month':
        $start_date = new DateTime('first day of this month');
        $end_date = new DateTime('last day of this month');
    break;
    case 'last_month':
        $start_date = new DateTime('first day of next month');
        $end_date = new DateTime('last day of next month');
    break;
    
    echo $start_date->format(DATE_FORMAT);
    echo $end_date->format(DATE_FORMAT);
    

    And if you want to get the first and last days of quarter, try to use:

     $start_date = new DateTime('first day of January');
     $end_date = new DateTime('last day of March');
    
     echo $start_date->format(DATE_FORMAT);
     echo $end_date->format(DATE_FORMAT);
    

    Or use function strtotime. Example with strtotime:

    $quarter_start = strtotime('first day of January');
    $quarter_end = strtotime('last day of March');
    
    echo date(DATE_FORMAT, $quarter_start);
    echo date(DATE_FORMAT, $quarter_end);
    

提交回复
热议问题