【问题标题】:Have calendar function grab only month and year有日历功能只抓取月份和年份
【发布时间】:2015-02-02 19:30:59
【问题描述】:

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

日期格式:YYYY-MM-DD 我希望它只做 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);
}

【问题讨论】:

    标签: php laravel calendar


    【解决方案1】:

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

    您只需在模型中添加accessor 即可格式化日期:

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

    【讨论】:

    • 抱歉回复晚了,我想看看这个解决方案是否有效,但没有。
    • 没问题。但如果你想让我帮助你,我需要更多的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多