碳添加时间问题

发布于 2025-01-19 21:46:40 字数 1830 浏览 0 评论 0原文

我很难理解为什么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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2025-01-26 21:46:40

我认为 H:m 是错误的,它应该像这样

    $endTime = $calculateReservationDates->format('H:i');

m 是指月份,而不是分钟。请使用i

我希望它有帮助

I think the H:m is wrong it's should be like this

    $endTime = $calculateReservationDates->format('H:i');

m is refer to month, not minute. For minute, use i.

I hope it's helpful

昔日梦未散 2025-01-26 21:46:40
// 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),
}
// 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);

Result with an hour added

Carbon\Carbon @1649331000 {#1724
   date: 2022-04-07 11:30:00.0 UTC (+00:00),
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文