[英]What's the best logic for grouping objects by closest start and end dates?
[英]Laravel 3 - What's the best way to query start and end dates that are the same day
我有一个用Laravel 3编写的Web应用程序。在此应用程序中,模型之一是“活动”。 这些活动都有开始日期和结束日期字段。 我想知道最好的方法是查询同一天开始和结束的所有活动吗?
- 更新 -
我正在使用mySQL并且字段类型均为Timestamp ...
我目前所拥有的 :
...
$activitySchedule = ActivitySchedule::with(array('location'))->where(function($query) {
// Query activities that start and end today
$yesterday = date('Y-m-d', strtotime('today')) . ' 00:00:00';
$tomorrow = date('Y-m-d', strtotime('today')) . ' 23:59:59';
$query->where('starts', '>', $yesterday);
$query->where('ends', '<', $tomorrow);
});
...
我认为这可以正常工作,但我想知道是否有更精确的方法?
您应该使用Carbon软件包,它可以让您像这样获得startOftheDay
和endoftheday
:
$StartofDay = Carbon::now()->startOfDay();
$EndofDay = Carbon::now()->endOfDay();
将其放置到位后,使用涉及条件的where条件查询模型,以使用StartofDay
和EndofDay
值以及created_at
列进行检查。
在这里找到碳包装: https : //github.com/briannesbitt/Carbon
希望能帮助到你。!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.