Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bekaskaki's avatar

Logic checkin method

I built a hotel booking system but i have a problem with the checkin method if I do checkin and checkout, for example:

check in date: 2019-09-17 00:22:00
checkout date: 2019-09-17 12:00:00

the price_total is 0

but if I do checkin and checkout:

checkin date: 2019-09-17 00:22:00
check date: 2019-09-18 12:00:00
the price_total is as I expected

controller :

public function store(Request $request)
{
    $data = new RoomTransaction();
    $ym = Carbon::now()->format('Y/m');
    $row = RoomTransaction::withTrashed()->get()->count() > 0 ? RoomTransaction::withTrashed()->get()->count() + 1 : 1;
    $no_invoice = $ym.'/INV-'.Helper::ref($row);
    $data->invoice_id = $no_invoice;
    $data->guest_id = $request->guest_id;
    $data->adult = $request->adult;
    $data->child = $request->child;
    $data->checkin_date = $request->checkin_date.' '.$request->time_checkin;
    $data->checkout_date = $request->checkout_date.' '.$request->time_checkout;
    $data->room_id = $request->room_id;
    $data->deposite= $request->deposite;
    $data->user_id = Auth::user()->id;
    $data->method = $request->method;
    $data->status = 1;
    $room_id = $data->room_id;
    $room = Room::find($room_id);

    $diff_day = $this->diffday($data->checkin_date,$data->checkout_date);

    $data->price_total = $room->roomtype->price_night * $diff_day;

    if ($data->save()) {

        $room = Room::find($room_id);
        $room->status = 1;
        $room->save();
    }
    return redirect('admin/'.$this->title)->with('success', 'Check-In Success');

}
 private function diffday($checkin,$checkout)
    {
        $checkin = date_create($checkin);
        $checkout = date_create($checkout);
        $interval = date_diff($checkin, $checkout);

       return $interval->format('%a');

    }

I am confused what logic is best for overcoming the problem

0 likes
5 replies
Sti3bas's avatar

As far as I understand, the minimum length of booking is 1 day. If so, return 1 from diffDays method whenever the difference between dates is 0.

Snapey's avatar

Your diff in days might be 0.6 or 1.1 or 1.5 or 1.6 or 1.99 etc

You need to decide how you will charge for these - according to your business rules

bekaskaki's avatar

how do i return 1 if the difference day 0?

can you give me a sample code?

Sti3bas's avatar
Sti3bas
Best Answer
Level 53

You should probably always add one day to your diff, as format('%a') returns diff in days rounded down.

Try return (int)$interval->format('%a') + 1;

Please or to participate in this conversation.