在Laravel Eloquent的->where()中,我应该使用$carbonObject->getTimestamp()还是只使用$carbonObject?

fdx2calv  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(54)

我不确定我是否应该直接在where()函数中使用Carbon date对象。或者我应该使用Carbon(date)对象的timestamp?

$todayMarketCloseTime = Carbon::now('Asia/Karachi')->hour(15)->minute(10)->second(00);

// Should I get the timestamp on the Carbon date object before passing it to where function?

$ratiosUpdatedAt = $todayMarketCloseTime->getTimestamp();

return Ratio::where('updated_at', '>=', $ratiosUpdatedAt)
->orderBy('ratio', 'desc')
->get();

字符串
我希望得到所有的日期fom比率表的更新日期大于或等于今天的股市收盘时间。

cetgtptt

cetgtptt1#

你可以使用这两种方法中的任何一种,这取决于你的偏好。然而,你可能会认为使用Carbon date对象更容易阅读,可以让你的代码更容易理解,
直接使用Carbon date对象:

$todayMarketCloseTime = Carbon::now('Asia/Karachi')
    ->hour(15)->minute(10)->second(00);

return Ratio::where('updated_at', '>=', $todayMarketCloseTime)
    ->orderBy('ratio', 'desc')
    ->get();

字符串

7kqas0il

7kqas0il2#

在Laravel的Eloquent中,日期比较通常使用数据库的本地日期格式进行。如果您使用典型的updated_at列,则它是datetime类型的列,将以YYYY-MM-DD HH:MM:SS格式存储值。
在给定的代码中,您不需要使用getTimestamp()将Carbon对象转换为时间戳。相反,您应该使用Carbon为MySQL日期时间类型提供的默认字符串表示。

$todayMarketCloseTime = Carbon::now('Asia/Karachi')->hour(15)->minute(10)->second(00);

return Ratio::where('updated_at', '>=', $todayMarketCloseTime)
    ->orderBy('ratio', 'desc')
    ->get();

字符串

相关问题