【问题标题】:Get all the services of the current day on Laravel在 Laravel 上获取当天的所有服务
【发布时间】:2015-04-07 06:52:51
【问题描述】:

我正在构建一个查询以获取特定motoboy 的所有servicios,但我需要它们是当前的。该属性的数据类型是timestamp。我有这个代码,但没有工作

Motoboy::with(array('servicios' => function($query){
                    $query->where('id_estado', '1')->orWhere('id_estado', '2')
                    ->where('fecha_hora', new DateTime('today'));

                }))->where('auth_token',$auth)->firstOrFail();

【问题讨论】:

  • 日期应作为字符串 date('Y-m-d H:i:s', time()') 传递。使用mysql Between获取两个时间戳之间的数据

标签: php mysql sql laravel laravel-4


【解决方案1】:

new DateTime 正在实例化一个对象,但不具备timestamp 所需的格式。 一个简单的解决方案是使用date() 函数来指定您想要的格式,因此对于当前的timestamp,您将使用date('Y-m-d H:i:s')

另外,我建议您像这样查询一整天:

$startToday = date('Y-m-d 00:00:00');
$endToday = date('Y-m-d 23:59:59');
// ...
->whereBetween('fecha_hora', array($startToday,$endToday));
// ....

你的代码会变成这样:

Motoboy::with(array('servicios' => function($query){
    $startToday = date('Y-m-d 00:00:00');
    $endToday = date('Y-m-d 23:59:59');
    $query->where('id_estado', '1')->orWhere('id_estado', '2')->whereBetween('fecha_hora', array($startToday,$endToday));

}))->where('auth_token',$auth)->firstOrFail();

【讨论】:

  • 用这个hora返回一个服务.."fecha_hora": "2019-02-08 05:02:00",
猜你喜欢
  • 1970-01-01
  • 2013-04-05
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 2021-12-04
  • 2017-03-17
相关资源
最近更新 更多