1
我正在創建一個在線預訂系統。當用戶點擊日曆中的日期時,它會返回該日期的兩個日期時間(開始和結束)。我試圖計算從開始到結束的所有小時數,但我需要間隔顯示小時數。如何計算PHP中兩個日期時間的小時間隔?
比方說,用戶已添加可用時間從明天10.00-14.00然後我需要顯示的時間是這樣的:
10.00-11.00
11.00-12.00
12.00-13.00
13.00-14.00
具體的一天。
我到目前爲止。
public function getTimes()
{
$user_id = Input::get("id"); //get the user id
$selectedDay = Input::get('selectedDay'); // We get the data from AJAX for the day selected, then we get all available times for that day
$availableTimes = Nanny_availability::where('user_id', $user_id)->get();
// We will now create an array of all booking datetimes that belong to the selected day
// WE WILL NOT filter this in the query because we want to maintain compatibility with every database (ideally)
// For each available time...
foreach($availableTimes as $t => $value) {
$startTime = new DateTime($value->booking_datetime);
if ($startTime->format("Y-m-d") == $selectedDay) {
$endTime = new DateTime($value->booking_datetime);
date_add($endTime, DateInterval::createFromDateString('3600 seconds'));
// Try to grab any appointments between the start time and end time
$result = Nanny_bookings::timeBetween($startTime->format("Y-m-d H:i"), $endTime->format("Y-m-d H:i"));
// If no records are returned, the time is okay, if not, we must remove it from the array
if($result->first()) {
unset($availableTimes[$t]);
}
} else {
unset($availableTimes[$t]);
}
}
return response()->json($availableTimes);
}
如何獲取間隔?
它正在使用普通p生命值。你能給我一個例子如何返回日期爲JSON? – raqulka
@raqulka你可以使用json_encode()爲$ times數組返回json數據,如更新後的答案 –
賞金是你的先生。謝謝。 – raqulka