在Date字段的非对象上调用成员函数

时间:2016-03-14 10:58:11

标签: php laravel

我需要使用addMonths()函数找出前几个月。但是在这样做时,它给了我错误"调用非对象的成员函数"。

以下是我的控制器代码段:

 <?php

 class ModelGridController extends Controller
 {

   public function index()
  {

    $title = "Model as Stock Market Grid" ;

    $latestDatetime =  Salemodel::select(DB::raw('max(Datetime) as date'))->get();
    $latestDate = $latestDatetime[0]->date;

    $previousDatetime =  Salemodel::select(DB::raw("max(CASE WHEN (Datetime < '$latestDate') THEN Datetime END) as date" ))->get();
    $previousDate = $previousDatetime[0]->date;

    $currentDatetime = \Carbon\Carbon::now();
    $currentYear = $currentDatetime->format("Y");

    $date = strtotime($latestDate);
    $newformat = date('y-m-d:H:i:s',$date);
    $threeMonthBefore = $date->addMonths(-3);
    //$sixMonthBefore = $date->addMonths(-6);
    //$twelveMonthBefore = $date->addMonths(-12);

     echo  $date ."  ".$newformat ;

     echo $latestDate ." ".$previousDate." ".$currentYear." ";      
  }

}

如何纠正这个问题?

0 个答案:

没有答案