碳添加时间问题
Carbon add hours issue
我很难理解为什么 Carbon addHours() 没有 return 正确的时间。
这是我控制器中的内容:
public function create(Request $request)
{
$locationId = $request->input('location_id');
$beginningDate = $request->input('beginning_date');
$beginningTime = $request->input('beginning_time');
// $beginningDate = $request->input('beginning_date')->format('d-m-Y');
// $beginningTime = $request->input('beginning_time')->format('H:m');
$duration = $request->input('duration');
$totalPrice=PriceList::where('duration_to_hours', $duration)->value('price');
$reservationStartingDate = $beginningDate. ','.$beginningTime;
$calculateReservationDates = Carbon::parse($reservationStartingDate)->addHours($duration);
$endDate= $calculateReservationDates->format('d-m-Y');
$endTime = $calculateReservationDates->format('H:m');
$reservedCount = Reservation::where('beginning_date', '>=', $endDate)
->where('end_date', '>=', $beginningDate )
->where('beginning_time', '<=', $endTime)
->where('end_time', '>=', $beginningTime)
->count();
$totalBoxes = Box::where('location_id', $locationId)->count();
dd($endDate, $endTime);
$available = false;
if($reservedCount < $totalBoxes){
$available = true;
}
return view('checkout', compact('locationId', 'totalPrice', 'endDate', 'endTime', 'beginningDate', 'beginningTime', 'duration', 'available'));
}
但是,如果例如开始时间是 10:50 上午并且持续时间是一小时,根据我的逻辑它应该 return $endTime 是 11.50 但它只是 returning 11.04.
我不明白我错过了什么,你知道吗?
谢谢
更新
这是 $reservationStartingDate
的 dd() :
^ "2022-04-22,00:53"
我认为H:m
是错误的应该是这样的
$endTime = $calculateReservationDates->format('H:i');
m
是指月份,不是分钟。对于分钟,使用 i
.
希望对您有所帮助
// Y - year
// m - month - not minutes
// d - day
// H - hours in 24h format default UTC
// i - minutes
// s - seconds
$beginningDate = '2022-04-07';
$beginningTime = '10:30'; // 24h format UTC
// "2022-04-07 10:30" - you need this format
$reservationStartingDate = "$beginningDate $beginningTime";
$result = Carbon::createFromFormat('Y-m-d H:i', $reservationStartingDate)->addHours(1);
添加了一个小时的结果
Carbon\Carbon @1649331000 {#1724
date: 2022-04-07 11:30:00.0 UTC (+00:00),
}
我很难理解为什么 Carbon addHours() 没有 return 正确的时间。
这是我控制器中的内容:
public function create(Request $request)
{
$locationId = $request->input('location_id');
$beginningDate = $request->input('beginning_date');
$beginningTime = $request->input('beginning_time');
// $beginningDate = $request->input('beginning_date')->format('d-m-Y');
// $beginningTime = $request->input('beginning_time')->format('H:m');
$duration = $request->input('duration');
$totalPrice=PriceList::where('duration_to_hours', $duration)->value('price');
$reservationStartingDate = $beginningDate. ','.$beginningTime;
$calculateReservationDates = Carbon::parse($reservationStartingDate)->addHours($duration);
$endDate= $calculateReservationDates->format('d-m-Y');
$endTime = $calculateReservationDates->format('H:m');
$reservedCount = Reservation::where('beginning_date', '>=', $endDate)
->where('end_date', '>=', $beginningDate )
->where('beginning_time', '<=', $endTime)
->where('end_time', '>=', $beginningTime)
->count();
$totalBoxes = Box::where('location_id', $locationId)->count();
dd($endDate, $endTime);
$available = false;
if($reservedCount < $totalBoxes){
$available = true;
}
return view('checkout', compact('locationId', 'totalPrice', 'endDate', 'endTime', 'beginningDate', 'beginningTime', 'duration', 'available'));
}
但是,如果例如开始时间是 10:50 上午并且持续时间是一小时,根据我的逻辑它应该 return $endTime 是 11.50 但它只是 returning 11.04.
我不明白我错过了什么,你知道吗?
谢谢
更新
这是 $reservationStartingDate
的 dd() :
^ "2022-04-22,00:53"
我认为H:m
是错误的应该是这样的
$endTime = $calculateReservationDates->format('H:i');
m
是指月份,不是分钟。对于分钟,使用 i
.
希望对您有所帮助
// Y - year
// m - month - not minutes
// d - day
// H - hours in 24h format default UTC
// i - minutes
// s - seconds
$beginningDate = '2022-04-07';
$beginningTime = '10:30'; // 24h format UTC
// "2022-04-07 10:30" - you need this format
$reservationStartingDate = "$beginningDate $beginningTime";
$result = Carbon::createFromFormat('Y-m-d H:i', $reservationStartingDate)->addHours(1);
添加了一个小时的结果
Carbon\Carbon @1649331000 {#1724
date: 2022-04-07 11:30:00.0 UTC (+00:00),
}