如何使用Carbon在PHP中创建日期字符串到日期对象?
$minTime = DB::table('order')
->select('order_creation_date', 'restaurant_id')
->where('restaurant_id', '=', '70')
->orderBy('order_creation_date', 'asc')
->first();
$m = $orderDay -> month;
$d = $orderDay -> day;
$y = $orderDay -> year;
$y = (int)$y;
$d = (int)$d;
$m = (int)$m;
$orderDay = $minTime -> order_creation_date;
var_dump($orderDay);
错误:
Trying to get property of non-object
字符串的正常值:
string(19) "2016-02-05 14:51:30"
任何想法如何解决?我想我需要make string to date对象。谢谢。
答案 0 :(得分:1)
在$dates
模型中使用Order
属性:
protected $dates = ['order_creation_date'];
答案 1 :(得分:0)
您的错误肯定在第2行
更改代码的顺序:
$minTime = DB::table('order')
->select('order_creation_date', 'restaurant_id')
->where('restaurant_id', '=', '70')
->orderBy('order_creation_date', 'asc')
->first();
$orderDay = $minTime -> order_creation_date;
$m = $orderDay -> month;
$d = $orderDay -> day;
$y = $orderDay -> year;
$y = (int)$y;
$d = (int)$d;
$m = (int)$m;
var_dump($orderDay);
或使用DB facade访问日期时,它可能不会返回Carbon实例。 试试......:
$orderDay = Carbon::createFromFormat("Y-m-d H:i:s",$minTime -> order_creation_date);
或者,您更应该在$dates
模型中使用Order
属性..
protected $dates = ['order_creation_date'];