obtenha startdate e enddate para o quarto atual php

Estou tentando definir uma data de início e uma data de término no trimestre.

Por exemplo, estou trabalhando em um sistema de relatórios onde eu preciso relatar os dados para o quarto trimestre, o quarto trimestre, o quarto trimestre eo quarto trimestre.

Quarto um – janeiro – março

Quarto dois – abril – junho

Trimestre Três – Julho – Setembro

Quarto quarto – outubro – dezembro

Eu tenho, por exemplo, alguns casos para o mês atual e o mês anterior como mostrado abaixo.

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; 

Mas agora eu preciso adicionar casos para este e no último trimestre e não tenho certeza de como realmente fazer isso para refletir o intervalo do quarto apropriado.

Alguma ideia?

Verifique isso para 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; 

Atualização: 2 e no 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; 
 /** * Compute the start and end date of some fixed o relative quarter in a specific year. * @param mixed $quarter Integer from 1 to 4 or relative string value: * 'this', 'current', 'previous', 'first' or 'last'. * 'this' is equivalent to 'current'. Any other value * will be ignored and instead current quarter will be used. * Default value 'current'. Particulary, 'previous' value * only make sense with current year so if you use it with * other year like: get_dates_of_quarter('previous', 1990) * the year will be ignored and instead the current year * will be used. * @param int $year Year of the quarter. Any wrong value will be ignored and * instead the current year will be used. * Default value null (current year). * @param string $format String to format returned dates * @return array Array with two elements (keys): start and end date. */ public static function get_dates_of_quarter($quarter = 'current', $year = null, $format = null) { if ( !is_int($year) ) { $year = (new DateTime)->format('Y'); } $current_quarter = ceil((new DateTime)->format('n') / 3); switch ( strtolower($quarter) ) { case 'this': case 'current': $quarter = ceil((new DateTime)->format('n') / 3); break; case 'previous': $year = (new DateTime)->format('Y'); if ($current_quarter == 1) { $quarter = 4; $year--; } else { $quarter = $current_quarter - 1; } break; case 'first': $quarter = 1; break; case 'last': $quarter = 4; break; default: $quarter = (!is_int($quarter) || $quarter < 1 || $quarter > 4) ? $current_quarter : $quarter; break; } if ( $quarter === 'this' ) { $quarter = ceil((new DateTime)->format('n') / 3); } $start = new DateTime($year.'-'.(3*$quarter-2).'-1 00:00:00'); $end = new DateTime($year.'-'.(3*$quarter).'-'.($quarter == 1 || $quarter == 4 ? 31 : 30) .' 23:59:59'); return array( 'start' => $format ? $start->format($format) : $start, 'end' => $format ? $end->format($format) : $end, ); } 

Eu desenvolvo essa function para lidar com o quarto de qualquer maneira: relativa (esta, anterior, primeira, última) e corrigida.

Exemplos:

 get_dates_of_quarter(); //return current quarter start and end dates get_dates_of_quarter(2); //return 2nd quarter start and end dates of current year get_dates_of_quarter('first', 2010, 'Ym-d'); //return start='2010-01-01' and end='2014-03-31' get_dates_of_quarter('current', 2009, 'Ym-d'); //Supposing today is '2014-08-22' (3rd quarter), this will return //3rd quarter but of year 2009. //return start='2009-07-01' and end='2009-09-30' get_dates_of_quarter('previous'); //Supposing today is '2014-02-18' (1st quarter), this will return //return start='2013-10-01' and end='2013-12-31' 

Esperando que isso ajude alguém;)

sobre o quê:

 $offset = (date('n')%3)-1; // modulo ftw $start = new DateTime("first day of -$offset month midnight"); $offset = 3-(date('n')%3); // modulo ftw again $end = new DateTime("last day of +$offset month midnight"); 

Código simples:

 $current_quarter = ceil(date('n') / 3); $first_date = date('Ym-d', strtotime(date('Y') . '-' . (($current_quarter * 3) - 2) . '-1')); $last_date = date('Ym-t', strtotime(date('Y') . '-' . (($current_quarter * 3)) . '-1')); 

Tente usar a function DateTime . Para o seu exemplo, parece:

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

E se você deseja obter os primeiros e últimos dias do trimestre, tente usar:

  $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); 

Ou use a function strtotime . Exemplo com 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); 

Algumas respostas são muito complicadas 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)); } 

Exemplo simples:

 define('DATE_FORMAT', 'Ym-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

Isso poderia ser muito mais simples, penso eu.

 function get_this_quarter() { $current_month = date('m'); $current_quarter_start = ceil($current_month/4)*3+1; // get the starting month of the current quarter $start_date = date("Ymd H:i:s", mktime(0, 0, 0, $current_quarter_start, 1, date('Y') )); $end_date = date("Ymd H:i:s", mktime(0, 0, 0, $current_quarter_start+3, 1, date('Y') )); // by adding or subtracting from $current_quarter_start within the mktime function you can get any quarter of any year you want. return array($start_date, $end_date); } 

Isso funciona tão bom sem todas as declarações if e é muito mais flexível. Conforme mencionado nos comentários dentro do código, você pode modificar facilmente o $ current_quarter_variable para atender às suas necessidades.

Espero que isto ajude!

 function getDates( $passedDate = '' ) { if( $passedDate == '' ) { $v = ceil( date( "m" ) / 3 ); $y = date( "Y" ); } else { $v = ceil( $month / 3 ); $y = date( "Y", strtotime( $passedDate ) ); } $y = date( "Y" ); $m = ( $v * 3 ) - 2; $date = $y . '-' . $m . '-' . 01; $return['begin'] = date( "Ymd", strtotime( $date ) ); $return['end'] = date( "Ymt", strtotime( $return['begin'] . "+ 2 months" ) ); $return['version'] = $y . 'Q' . ceil( date( "m" ) / 4 ); return $return; } 

Esta function retornará a data de início e término e trimestre para a data atual por padrão, no entanto, se você quiser para determinada data, apenas passe o mês para isso.

Esta foi a minha solução.

 function getCurrentQuarter($timestamp=false){ if(!$timestamp)$timestamp=time(); $day = date('n', $timestamp); $quarter = ceil($day/3); return $quarter; } function getPreviousQuarter($timestamp=false){ if(!$timestamp)$timestamp=time(); $quarter = getCurrentQuarter($timestamp) - 1; if($quarter < 0){ $quarter = 4; } return $quarter; } function getNextQuarter($timestamp=false){ if(!$timestamp)$timestamp=time(); $quarter = getCurrentQuarter($timestamp) + 1; if($quarter > 4){ $quarter = 0; } return $quarter; } function getFirstAndLastDayOfQuarter($quarter, $year=false){ if(!$year)$year=date('Y'); $startDate = new DateTime($year.'-'.($quarter*3-2).'-1'); //Get first day of first month of next quarter $endMonth = $quarter*3+1; if($endMonth>12){ $endMonth = 1; $year++; } $endDate = new DateTime($year.'-'.$endMonth.'-1'); //Subtract 1 second to get last day of prior month $endDate->sub(new DateInterval('PT1S')); return array($startDate, $endDate); } 

Você pode simplesmente usar isso basicamente usando matemática básica.

Cada número de mês menos 1% 3 indicará quantos meses você está deslocado do trimestre atual.

 /** * @param DateTime $date * @return array */ function getQuarterRangeFromDate(DateTime $date) { // Clone the date to avoid modifying your date in current context $quarter_start = clone($date); // Find the offset of months $months_offset = ($date->format('m') - 1) % 3; // Modify quarter date $quarter_start->modify(" - " . $months_offset . " month") ->modify("first day of this month"); $quarter_end = clone($quarter_start); $quarter_end->modify("+ 3 month"); return [$quarter_start, $quarter_end]; } 

Acabei com esta solução simples:

 $curQuarter = ceil(date("m")/3); $startDate = date("Y") . "-" . $curQuarter*3-2 . "-01"; $endDate = date("Y") . "-" . $curQuarter*3 . "-31"; 

(Sim, alguns meses não terão 31 dias, mas no meu caso não importava)

 $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"); 

Uma maneira mais simples de obter o início / fim do trimestre atual nas datas:

 $qtr_start = date("Y-".(ceil(date("n")/3))."-01"); 

Então, apenas adicione 3 meses no lugar correto para obter o fim:

 $qtr_end = date("Y-".(ceil(date("n")/3)+3)."-01"); 

Apenas meus dois centavos.