1
我在預訂應用程序的工作,我從用戶那裏得到一個開始和結束(日期時間),並檢查這個期間(開始&末)與數據庫中的列(開始&結束)相交,所以我需要比較Input['start']
和Input['end']
與數據庫,所以我用Carbon::create($inputstart)->between($start ,$end));
但alwayes給errores「意外的數據」 &「尾隨數據」Laravel Carbon,如何格式化以比較日期時間?
這裏是控制器的方法來查看和存儲輸入
public function store(Request $request,$yard,$court)
{
$start =$request->input("start");
$end =$request->input("end");
$courtCalendar = CalendarEvent::where('court_id',$court)->get();
$start= Carbon::parse($start);
$end = Carbon::parse($end);
foreach ($courtCalendar as $booked) {
$bend= Carbon::parse($booked->end);
$bstart= Carbon::parse($booked->start);
dd(Carbon::create($end)->between($bstart ,$bend));
if (!Carbon::create($start)->between($bstart ,$bend)){
if (!Carbon::create($end)->between($bstart ,$bend)) {
$calendar_event = new CalendarEvent();
$calendar_event->user_id = Auth::user()->id;
$calendar_event->court_id = $court;
$calendar_event->title = $request->input("title");
$calendar_event->start = $start;
$calendar_event->end = $end;
$calendar_event->status = 0;
$calendar_event->save();
return redirect()->back()
->with(['header'=>'Success!','class' =>'alert alert-success','message'=> 'booked successfully.',
'calendar'=>$calendar_event]);
}else
{return redirect()->back()
->with(['header'=>'Warning!','class' =>'alert alert-warning','message'=> 'cannot book at this time end time is occupied']);}
}else
{return redirect()->back()
->with(['header'=>'Warning!','class' =>'alert alert-warning','message'=> 'cannot book at this time start time is occupied']);}
}
}
我也宣告了數據庫列在模型中,日期爲
protected $dates = ['start', 'end'];
如何從HTML表單和數據庫「datetime」傳遞datetime格式,以便Carbon可以執行「between」比較?
謝謝@MMMTroy,它的工作 –
謝謝[MMMTroy](http://stackoverflow.com/a/41002733/3452348)這個超級回答。 @BeshoyTamry請授予他[接受的答案](http://meta.stackexchange.com/a/5235/294451) – Zl3n