碳添加时间问题
我很难理解为什么Carbon AddHours()没有返回适当的时间。
这就是我在控制器中所拥有的:
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 am,并且持续时间为一个小时,基于我的逻辑,它应该返回$ endtime为11.50,但仅返回11.04。
我没有得到我缺少的东西,你有什么想法吗?
谢谢
更新,
这是$ recervationstartingdate
的dd():
^ "2022-04-22,00:53"
I'm struggling to understand why Carbon addHours() is not returning the proper time.
This is what i have in my controller :
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'));
}
But if for example the beginning time is 10:50 am and the duration is one hour, based on my logic it should return the $endTime to be 11.50 but it's only returning 11.04.
I do not get what I'm missing, do you have any idea?
Thank you
UPDATE
Here's the dd() of $reservationStartingDate
:
^ "2022-04-22,00:53"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
H:m
是错误的,它应该像这样m
是指月份,而不是分钟。请使用i
。我希望它有帮助
I think the
H:m
is wrong it's should be like thism
is refer to month, not minute. For minute, usei
.I hope it's helpful
结果添加了一个小时
Result with an hour added