2017-08-21 38 views
0

我正在預訂系統上工作,並且只需要選擇當前某個字段的記錄。不是今天和將來,但只有今天。PHP Carbon/Laravel雄辯 - 檢查日期時間值是否爲當天

查詢當前是:

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

$advancedBookings->get(); 

的requested_booking_time是希望要檢查的領域,存儲日期的格式如下:

2017-08-23 08:00:00 

所以我想只能選擇行與當天同日。

回答

1

據我所知,你想要的是今天創建的記錄;那麼只需獲得今天的日期。

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

現在您的查詢是這樣的。

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

@Lovelock如果重要,還要考慮TZ差異。 – Tpojka

0

你應該試試這個:

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

希望這對你的工作!

0

您可以使用whereDateCarbon格式化日期:

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

$advancedBookings->get(); 

或者與Carbon查詢之前格式化你的日期。

相關問題