【问题标题】:PHP Carbon / Laravel Eloquent - Check if DateTime value is for the current dayPHP Carbon / Laravel Eloquent - 检查 DateTime 值是否为当天
【发布时间】:2018-01-29 14:56:13
【问题描述】:

我正在开发一个预订系统,并且只需要选择今天某个字段所在的记录。不是今天和未来,而是今天。

当前查询是:

$advancedBookings = Booking::where('type', 2)
    ->whereNull('estimated_booking_time')
    ->where('requested_booking_time', ?????);

$advancedBookings->get();

requested_booking_time 是我希望检查的字段,存储的日期是这样的格式:

2017-08-23 08:00:00

所以我只想选择与当天在同一天的行。

【问题讨论】:

    标签: php sql laravel eloquent php-carbon


    【解决方案1】:

    你应该试试这个:

    $advancedBookings = Booking::where('type', 2)
        ->whereNull('estimated_booking_time')
        ->whereDate('requested_booking_time', '=', date('Y-m-d'))
        ->get();
    

    【讨论】:

      【解决方案2】:

      据我了解,您想要今天创建的记录;然后获取今天的日期。

      $today = date("Y-m-d");
      

      现在你的查询是这样的。

      $advancedBookings = Booking::where('type', 2)
          ->whereNull('estimated_booking_time')
          ->where('requested_booking_time', $today)->get();
      

      【讨论】:

      • @Lovelock 如果重要,还要考虑 TZ 差异。
      【解决方案3】:

      您可以使用whereDate 并使用Carbon 格式化您的日期:

      $advancedBookings = Booking::where('type', 2)
          ->whereNull('estimated_booking_time')
          ->whereDate('requested_booking_time', '=' , Carbon::today()->toDateString());
      
      $advancedBookings->get();
      

      或者在查询之前使用Carbon格式化您的日期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-29
        • 2014-12-22
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        • 2020-01-14
        • 2016-07-01
        相关资源
        最近更新 更多