get startdate and enddate for current quarter php

前端 未结 19 1547
悲哀的现实
悲哀的现实 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:28
    $QuarterMonth = ["1" => ["Y-01-01","Y-03-31"],"2"=>["Y-04-01","Y-06-30"],"3"=>["Y-07-01","Y-09-30"],"4"=>["Y-10-01","Y-12-31"]];
    $curMonth = date("m", time());
    
    $qtr_month_from = date($QuarterMonth[ceil($curMonth/3)][0]);
    $qtr_month_to = date($QuarterMonth[ceil($curMonth/3)][1]);
    
    0 讨论(0)
  • 2020-12-29 11:31

    check this for this quarter.

     case 'this_quarter':
    
              $current_month = date('m');
              $current_year = date('Y');
              if($current_month>=1 && $current_month<=3)
              {
                $start_date = strtotime('1-January-'.$current_year);  // timestamp or 1-Januray 12:00:00 AM
                $end_date = strtotime('1-April-'.$current_year);  // timestamp or 1-April 12:00:00 AM means end of 31 March
              }
              else  if($current_month>=4 && $current_month<=6)
              {
                $start_date = strtotime('1-April-'.$current_year);  // timestamp or 1-April 12:00:00 AM
                $end_date = strtotime('1-July-'.$current_year);  // timestamp or 1-July 12:00:00 AM means end of 30 June
              }
              else  if($current_month>=7 && $current_month<=9)
              {
                $start_date = strtotime('1-July-'.$current_year);  // timestamp or 1-July 12:00:00 AM
                $end_date = strtotime('1-October-'.$current_year);  // timestamp or 1-October 12:00:00 AM means end of 30 September
              }
              else  if($current_month>=10 && $current_month<=12)
              {
                $start_date = strtotime('1-October-'.$current_year);  // timestamp or 1-October 12:00:00 AM
                $end_date = strtotime('1-January-'.($current_year+1));  // timestamp or 1-January Next year 12:00:00 AM means end of 31 December this year
              }
    
    
    
            break;
    

    Update : 2 and for last quarter

    case 'last_quarter':
    
              $current_month = date('m');
              $current_year = date('Y');
    
              if($current_month>=1 && $current_month<=3)
              {
                $start_date = strtotime('1-October-'.($current_year-1));  // timestamp or 1-October Last Year 12:00:00 AM
                $end_date = strtotime('1-January-'.$current_year);  // // timestamp or 1-January  12:00:00 AM means end of 31 December Last year
              } 
              else if($current_month>=4 && $current_month<=6)
              {
                $start_date = strtotime('1-January-'.$current_year);  // timestamp or 1-Januray 12:00:00 AM
                $end_date = strtotime('1-April-'.$current_year);  // timestamp or 1-April 12:00:00 AM means end of 31 March
              }
              else  if($current_month>=7 && $current_month<=9)
              {
                $start_date = strtotime('1-April-'.$current_year);  // timestamp or 1-April 12:00:00 AM
                $end_date = strtotime('1-July-'.$current_year);  // timestamp or 1-July 12:00:00 AM means end of 30 June
              }
              else  if($current_month>=10 && $current_month<=12)
              {
                $start_date = strtotime('1-July-'.$current_year);  // timestamp or 1-July 12:00:00 AM
                $end_date = strtotime('1-October-'.$current_year);  // timestamp or 1-October 12:00:00 AM means end of 30 September
              }
    
    
    
            break;
    
    0 讨论(0)
  • 2020-12-29 11:35
    $monthsFromStart = (date('n') - 1) % 3; // 0, 1, 2, 0, 1, 2, ...
    $monthsToEnd = 2 - $monthsFromStart; // 2, 1, 0, 2, 1, 0, ...
    
    $startDay = new DateTime("first day of -$monthsFromStart month midnight");
    $endDay = new DateTime("last day of +$monthsToEnd month midnight");
    
    0 讨论(0)
  • 2020-12-29 11:37

    Some answers are way too complicated IMO

    public function getStartOfQuarter()
    {
         return date(sprintf('Y-%s-01', floor((date('n') - 1) / 3) * 3 + 1));
    }
    
    public function getEndOfQuarter()
    {
        return date(sprintf('Y-%s-t', floor((date('n') + 2) / 3) * 3));
    }
    
    0 讨论(0)
  • 2020-12-29 11:37

    Simple example:

    define('DATE_FORMAT', 'Y-m-d');
    
    function get_start_and_end_date($case) {
        $start = 'first day of ';
        $end = 'last day of ';
    
        if ($case == 'this_quarter') {
            $case = 'quarter_' . ceil((new DateTime)->format('n') / 3);
        }
    
        switch ($case) {
            case 'prev_month'    : $start .= 'previous month'; $end .= 'previous month'; break;
            default              :
            case 'this_month'    : $start .= 'this month';     $end .= 'this month';     break;
            case 'next_month'    : $start .= 'next month';     $end .= 'next month';     break;
            case 'first_quarter' :
            case 'quarter_1'     : $start .= 'January';        $end .= 'March';          break;
            case 'quarter_2'     : $start .= 'April';          $end .= 'June';           break;
            case 'quarter_3'     : $start .= 'July';           $end .= 'September';      break;
            case 'last_quarter'  :
            case 'quarter_4'     : $start .= 'October';        $end .= 'December';       break;
        }
    
        return [
            'start' => (new DateTime($start))->format(DATE_FORMAT),
            'end' => (new DateTime($end))->format(DATE_FORMAT),
        ];
    }
    

    demo

    0 讨论(0)
  • 2020-12-29 11:40

    I glimpsed over answers and all are about quarters of this year. Sometimes, you need quarter for particular date. Here is my attempt with detailed explanation what-does-what in code comments:

    /**
         * get quarter begin/end date
         * 
         * possible usage:
         * quarter start date: quarter('2016-04-12')[start] - result: 2016-04-01
         * quarter end date: quarter('2016-04-12')[end] - result: 2016-06-30
         * quarter number: quarter('2016-04-12')[number] - result: 2
         * 
         * @param  string   $time   date you want quarter begin and end for - accepted formats, at least: 'Y-m', but also: 'Y-m-d', 'Y-m-d H:i:s' etc.
         * @return array    [quarter start, quarter end, quarter number]
         */
        public function quarter($time)
        {
            /**
             * get quarter number for given date
             * we are ceiling floating point value, so I use (int) casting to get just integer (without decimal part)
             * round() could be used instead of casting to (int)
             */
            $quarter = (int)ceil(date("m", strtotime($time)) / 3);
            $year = date("Y", strtotime($time));
    
            $quarters = [
                1 => ['01-01', '03-31', 1],
                2 => ['04-01', '06-30', 2],
                3 => ['07-01', '09-30', 3],
                4 => ['10-01', '12-31', 4],
            ];
    
            $quarterStart = $year . '-' . $quarters[$quarter][0];
            $quarterEnd = $year . '-' . $quarters[$quarter][1];
            $quarterNumber = $quarters[$quarter][2];
    
            return ['start' => $quarterStart, 'end' => $quarterEnd, 'number' => $quarterNumber];
        }
    
    0 讨论(0)
提交回复
热议问题