web-dev-qa-db-fra.com

obtenir les dates de début et de fin du trimestre en cours, php

J'essaie de fixer une date de début et une date de fin d'ici le trimestre.

Par exemple, je travaille sur un système de reporting dans lequel je dois rapporter des données pour les trimestres 1, 2, 3 et 4.

Premier trimestre - janvier - mars

Deuxième trimestre - avril - juin

Troisième trimestre - juillet - septembre

Quatrième trimestre - octobre - décembre

J'ai par exemple quelques cas pour le mois en cours et le mois précédent comme indiqué ci-dessous.

   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;

Mais maintenant, je dois ajouter des observations pour ceci et le dernier trimestre et je ne sais pas comment le faire, cela reflète donc la plage de trimestre appropriée.

Des idées?

10
priyanka patel

vérifiez ceci pour 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;

Mise à jour: 2 et pour 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;
8
Satish Sharma
/**
* 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,
    );
}

Je développe cette fonction pour traiter le quart de quelque manière que ce soit: relatif (ceci, précédent, premier, dernier) et fixe.

Exemples:

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, 'Y-m-d');
//return start='2010-01-01' and end='2014-03-31'

get_dates_of_quarter('current', 2009, 'Y-m-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'

En attendant cette aide quelqu'un;)

16
Delmo

qu'en est-il de:

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

Code simple:

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

Certaines réponses sont trop compliquées, OMI

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));
}
4
greg0ire

Essayez d'utiliser DateTime function. Pour votre exemple, voici:

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

Et si vous voulez obtenir les premier et dernier jours du trimestre, essayez d’utiliser:

 $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 utilisez la fonction strtotime . Exemple avec 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);
2
Dmitriy.Net

Cela pourrait être beaucoup plus simple, je pense.

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("Y-m-d H:i:s", mktime(0, 0, 0, $current_quarter_start, 1, date('Y') ));
    $end_date = date("Y-m-d 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);
}

Cela fonctionne aussi bien sans toutes les déclarations if et est beaucoup plus flexible. Comme mentionné dans les commentaires dans le code, vous pouvez facilement modifier la variable $ current_quarter_variable en fonction de vos besoins.

J'espère que cela t'aides!

2
base34

Exemple simple:

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

1
Glavić

Je voulais juste indiquer La solution de SynaTree ne fonctionne pas tous les 3 derniers mois du trimestre.

Voici une solution modifiée utilisant DateTime.

$now = new DateTimeImmutable();
$offset = ($now->format('n') - 1) % 3;
$start = $now->modify("first day of -{$offset} month midnight");
$endExcluded = $start->modify("+3 month");
$endIncluded = $start->modify("+3 month -1 second");

$endExcluded fonctionne bien pour les boucles DatePeriod, où la date de fin est exclue lorsque l'heure est 00:00:00.

1
FrankJaeger

Vous pouvez simplement cela grandement en utilisant les mathématiques de base.

Chaque nombre de mois moins 1% 3 indiquera combien de mois vous êtes compensé par rapport au trimestre en cours.

/**
 * @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];
}
0
Simon Ferragne
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( "Y-m-d", strtotime(  $date ) );
    $return['end'] = date( "Y-m-t", strtotime( $return['begin'] . "+ 2 months"  ) );
    $return['version'] = $y . 'Q' . ceil( date( "m" ) / 4 );
    return $return;
}

Cette fonction renverra par défaut la date de début et de fin et le trimestre pour la date actuelle. Toutefois, si vous le souhaitez pour une date donnée, il suffit de lui passer le mois.

0
Deejay

J'ai jeté un coup d'œil sur les réponses et toutes concernent les trimestres de cette année. Parfois, vous avez besoin de trimestre pour une date donnée. Voici ma tentative avec une explication détaillée que fait-on dans les commentaires de code:

/** 
 * Indiquer la date de début/fin du trimestre 
 * 
 * utilisation possible: 
 * date de début du trimestre: trimestre ('2016- 04-12 ') [début] - résultat: 2016-04-01 
 * Date de fin du trimestre: trimestre («2016-04-12») [fin] - résultat: 2016-06-30 
 * numéro du trimestre: trimestre ('2016-04-12') [nombre] - résultat: 2 
 * 
 * @param chaîne $ date du début et de la fin du trimestre pour - acceptée formats, au moins: 'Y-m', mais aussi: 'Ym-d', 'Ymd H: i: s' etc. 
 * @return array [début et fin du trimestre, numéro du trimestre] 
 */
 trimestre de la fonction publique ($ time) 
 {
 /**
 * obtenir le numéro du trimestre pour la date donnée 
 * nous sommes au plafond, donc j’utilise (int) transtypage pour obtenir uniquement un entier (sans partie décimale) 
 * round () peut être utilisé à la place de la conversion en (int) 
 */
 $ quarter = (int) ceil (date ("m", strtotime ($ time))/3); [._ ___.] $ année = date ("Y", strtotime ($ heure)); 
 
 $ trimestres = [
 1 => ['01-01 ', '03 -31 ', 1], 
 2 => ['04 - 01', '06 - 30', 2], 
 3 => ['07-01 ', '09 -30 ', 3], 
 4 => ['10-01', '12-30 ', 4], 
]; 
 
 $ QuarterStart = $ année . '-' $ quarter [$ quarter] [0]; 
 $ quarterEnd = $ year. '-' $ quarter [$ quarter] [1]; 
 $ quarterNumber = $ quarter [$ quarter] [2]; 
 
 return ['start' => $ quarterStart, 'end '=> $ quarterEnd,' number '=> $ quarterNumber]; 
} 
0
Jeffz
$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
Barvajz

C'était ma solution.

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);
}
0
mattferderer

J'ai fini avec cette solution simple:

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

(Oui, certains mois n'auront pas 31 jours mais dans mon cas, cela n'avait pas d'importance)

0
Yush0

J'avais besoin d'une fonction pour me donner les dates pour chaque trimestre afin de pouvoir récupérer des informations sur les ventes. Voici le code si quelqu'un en a besoin:

   private function getQuarterRange($quarter, $year=null) {
        if ($quarter > 4 || $quarter < 1)
            return null;

        if ($year == null)
            $year = date('Y');


        $startDate = date('Y-m-d', strtotime($year . '-' . (($quarter * 3) - 2). '-1'));
        $endDate = date('Y-m-d', strtotime(date('Y-m-d', strtotime($startDate)) . '+3 month - 1 day'));
        return ['startDate' => $startDate, 'endDate' => $endDate];
    }

Appelez simplement: var_dump($this->getQuarterRange(4, 2018)); Output:

array(2) {
  ["startDate"]=>
  string(10) "2018-10-01"
  ["endDate"]=>
  string(10) "2018-12-31"
}
0
Mil0s