Search code examples
phpdate

get startdate and enddate for current quarter php


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, quarter 3, and quarter 4.

Quarter One - January - March

Quarter Two - April - June

Quarter Three - July - September

Quarter Four - October - December

I have for example some cases for the current month, and the previous month as shown below.

   case 'this_month':
      $start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 1, date("Y")));
      $end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), date("d"), date("Y")));
    break;
    case 'last_month':
      $start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
      $end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 0, date("Y")));
    break;

But now i need to add cases for this and last quarter and I am not sure how to actually do that so it reflects the proper quarter range.

Any Ideas?


Solution

  • 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;