【问题标题】:Laravel using carbon to get current quarterLaravel 使用碳来获取当前季度
【发布时间】:2015-04-06 14:34:15
【问题描述】:

如何使用 Carbon 来确定当前季度? IE。我想知道季度开始的日期和结束的日期。

我尝试了直观的 echo new Carbon('this quarter'); 方式,但它不起作用,但我猜他们没有一个季度。


我想通了,我做到了:

$query->where(DB::raw('QUARTER(FT.created_at)'), Carbon::now()->quarter);
$query->where(DB::raw('YEAR(FT.created_at)'), '=', Carbon::now()->year);

但现在我正在为如何获取上一季度的开始和结束日期而苦苦挣扎。

【问题讨论】:

  • 您希望它返回什么日期?还是只想获取创建日期的当前季度数?
  • Carbon::now()->toDateString() 返回的日期
  • 您是否只想检查日期是否在上个季度?
  • @mininoz 是的,没错,这是我正在制作的过滤器。
  • @imperium2335 您正在使用QUARTER(FT.created_at),因此您可能不再需要担心季度的开始或结束日期。为什么不直接将 4 传递给查询?

标签: php laravel laravel-5 php-carbon


【解决方案1】:

您可以使用firstOfQuarterlastOfQuarter 方法来确定一个季度的开始和结束日期...

$date = new \Carbon\Carbon('-3 months');
$firstOfQuarter = $date->firstOfQuarter();
$lastOfQuarter = $date->lastOfQuarter();

【讨论】:

    【解决方案2】:

    我想我已经解决了:

    ...
    case 9:
                        $a = Carbon::now();
                        $a->month($a->month-3);
                        $lastQuarter = $a->quarter;
                        $query->where(DB::raw('QUARTER(FT.created_at)'), $lastQuarter);
                        $query->where(DB::raw('YEAR(FT.created_at)'), $a->year);
                        break;
    ...
    

    如果有更好的方法,请告诉我,非常感谢您的帮助。

    【讨论】:

      【解决方案3】:

      只是为了在上面的答案中添加更多内容,应该使用的实际方法是以下方法:

      $date = new \Carbon\Carbon('-3 months'); // for the last quarter requirement
      $date->startOfQuarter(); // the actual start of quarter method
      $date->endOfQuarter(); // the actual end of quarter method (with time 23:59:59.999999)
      

      以下内容并不完全正确:

      $date->firstOfQuarter(); /* use this method when you wish to get the first
                                  occurrence of a given day in the current quarter, its 
                                  fallback works similar to the startOfQuarter() method */
      $date->lastOfQuarter(); /* this is where the problem located, unlike the
                                 endOfQuarter() method, this method return the start of a
                                 day (with time 00:00:00.000000) (because the method is
                                 designed to get the last occurrence of a given day in the
                                 current quarter */
      

      【讨论】:

        【解决方案4】:
        • 以当前日期作为输入查找当前季度的开始和结束日期,并使用自定义格式设置日期的格式。

        **

        $yyyymm = 当前日期(); $date = new \Carbon\Carbon($yyyymm);
        $ProfileData['date_from'] = $date->firstOfQuarter()->format('Ymd');
        $ProfileData['date_to'] = $date->endOfQuarter()->format('Ymd');

        **

        【讨论】:

          【解决方案5】:

          返回给定日期的季度。

          $date = Carbon::parse($timestamp);
          $date->quarter;
          

          【讨论】:

            猜你喜欢
            • 2012-08-12
            • 2020-09-02
            • 1970-01-01
            • 1970-01-01
            • 2018-08-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-07
            相关资源
            最近更新 更多