【问题标题】:Selecting records that fall withing a time range in MySQL with Laravel 4使用 Laravel 4 选择 MySQL 中某个时间范围内的记录
【发布时间】:2013-09-24 18:40:27
【问题描述】:

大家好,我遇到了一个我无法弄清楚的问题。我有一个包含几列的数据库。标题、正文、开始和结束。

开始和结束列是日期时间类型。我的数据库中有包含开始日期和结束日期的行。

我正在尝试选择日期范围内的所有行。在本例中为 24 小时跨度。

例如,如果我有一行的开始日期为今天(2013 年 9 月 24 日),结束日期为 2014 年 1 月 1 日,我希望它会被返回。

Route::get('/', function()
{
//Start of Day
$start = date('Y-m-d 00:00:00');

//End of Day    
$end = date('Y-m-d 23:59:59'); 

$data['flyers'] = Flyer::where('start', '>=', $start)->where('end', '<=', $end)->get();

//return homepage
return View::make('view', $data);
});

提前致谢。如果我先弄清楚,我会发布我的答案。

【问题讨论】:

  • 开始列和结束列的数据类型是什么?
  • @extreme 它们是日期时间列。我正在尝试检索尚未过期且当前的事件。

标签: php mysql datetime select laravel


【解决方案1】:

where() 调用中的比较运算符是向后的。您的代码现在的方式是,您只选择今天开始和结束的传单。你想要的是这样的:

$data['flyers'] = Flyer::where('start', '<=', $start)->where('end', '>=', $end)->get();

【讨论】:

  • 谢谢,这似乎有效。你是一个绅士和一个学者。谢谢。
猜你喜欢
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多