获取当前季度 php 的开始日期和结束日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21185924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:53:24  来源:igfitidea点击:

get startdate and enddate for current quarter php

phpcodeigniterdate

提问by priyanka patel

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.

例如,我正在开发一个报告系统,我需要报告第 1 季度、第 2 季度、第 3 季度和第 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 quarterand I am not sure how to actually do that so it reflects the proper quarter range.

但是现在我需要为这个季度和上一个季度添加案例,我不确定如何实际做到这一点,因此它反映了正确的季度范围。

Any Ideas?

有任何想法吗?

采纳答案by Satish Sharma

check this for this quarter.

检查这个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 : 2and for last quarter

更新:2last 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;

回答by Delmo

/**
* 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,
    );
}

I develop this function to deal with quarter in any way: relative (this, previous, first, last) and fixed.

我开发了这个函数来以任何方式处理季度:相对(这个,前一个,第一个,最后一个)和固定。

Examples:

例子:

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'

Waiting this help someone ;)

等待这帮助某人;)

回答by SynaTree

what about:

关于什么:

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

回答by KLOZ

Simple code:

简单代码:

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

回答by greg0ire

Some answers are way too complicated IMO

一些答案太复杂了 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));
}

回答by FrankJaeger

Just wanted to point SynaTree's solution doesn't work for every last 3rd month in quarter.

只是想指出SynaTree的解决方案不适用于每季度的最后 3 个月。

Here's a modified solution using DateTime.

这是使用 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");

$endExcludedworks well when for DatePeriod loops, where the end date is excluded when time is 00:00:00.

$endExcluded在 for DatePeriod 循环中效果很好,其中结束日期在时间为 00:00:00 时被排除在外。

回答by Dmitriy.Net

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

尝试使用DateTime函数。对于您的示例,看起来像:

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:

或者使用函数strtotime。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);

回答by base34

This could be a whole lot simpler I think.

我认为这可能会简单得多。

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

This works just as good without all the if statements and is much more flexible. As mentioned in the comments within the code, you can easily modify the $current_quarter_variable to suit your needs.

这在没有所有 if 语句的情况下同样有效,并且更加灵活。如代码中的注释所述,您可以轻松修改 $current_quarter_variable 以满足您的需要。

Hope this helps!

希望这可以帮助!

回答by ptmr.io

A versatile version would be:

一个多功能的版本是:

    $date = new DateTime(/* you may insert a date here, else its now */);
    //$date->modify('-3 months'); // you may want last quarter or any other modifcation
    $quarter = ceil($date->format('n')/3);

    $start = new DateTime();
    $start->setDate($date->format('Y'), (($quarter*3)-2), 1)->setTime(0, 0, 0, 0);

    $end = new DateTime();
    $end->setDate($date->format('Y'), ($quarter*3), 1);
    $end->setDate($date->format('Y'), ($quarter*3), $end->format('t'))->setTime(0, 0, 0, 0);

    echo $start->format('Y-m-d');
    echo $end->format('Y-m-d');

this is simple yet effective. It let's you choose the input date and make relative adaptions with modify.

这是简单而有效的。它让您选择输入日期并通过修改进行相对调整。

回答by Glavi?

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

demo