为什么我不能为year_revenue获得错误的值

时间:2019-03-18 11:09:02

标签: php html mysql laravel

public function revenue(){
      $today = Carbon::today();
      $date = Carbon::createFromDate(2019, 3, 1);
      $daily_revenue = $this->getRevenue($today->format('Y-m-d 00:00:00'), $today->format('Y-m-d 23:59:59'));
      $monthly_revenue = $this->getRevenue(Carbon::today()->startOfMonth(), Carbon::today()->endOfMonth());
      $yearly_revenue = $this->getRevenue(Carbon::createFromDate()->startOfYear(), Carbon::createFromDate()->EndOfYear());
      dd($yearly_revenue);
      return view('layouts/home', ['']);
    }

    private function getRevenue($date_from, $date_to) {
      $revenue = Sales::whereBetween('created_at', [$date_from, $date_to])
        ->sum('grand_price');

      return $revenue;
    }

我已经测试过 每日收入和每月收入都可以  但是 yearly_revenue 我不知道该怎么做。请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以使用createFromDate代替now,这将为您提供当前年份:

$yearly_revenue = $this->getRevenue(Carbon::now()->startOfYear(), Carbon::now()->endOfYear());