具有日历功能仅抓取月份和年份


Have calendar function grab only month and year

如何让此函数仅根据年和月而不是年月和日显示项目?

日期格式:年-月-日我希望它只做 YYYY-MM

法典:

  public function calendar($month = null, $year = null)
  {
  $now = Carbon::now();
  $month = $month ? $month : $now->month;
  $year = $year ? $year : $now->year;
  $date = new Carbon("{$year}-{$month}");
  //get the current month range
  $start = $date->startOfMonth()->toDateString();
  $end   = $date->endOfMonth()->toDateString();
  $items = $this->model->whereNotNull('poster')
  ->where('release_date', '>=', $start)
  ->where('release_date', '<=', $end)
  ->orderBy('release_date', 'asc')
  ->cacheTags('calendar')
  ->remember(-1)
  ->get()
  ->toArray();
  return array('year' => $date->year, 'month' => $date->month, 'items' => $items);
}

如果我理解你的问题是正确的,现在只是关于演示文稿。(因为你已经使用startOfMonth()endOfMonth()来正确获取你的where语句)

您只需向模型添加访问器即可设置日期格式:

public function getReleaseDate(){
    return Carbon::parse($this->attributes['release_date'])->format('Y-m');
}