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

hupp's avatar
Level 11

Calculation of minimum booking price

I have 3 time slot like below

1Day = 10

1Week = 50

1Month = 200

And my booking period is 43 days.

Now I want a minimum price from above time slot

calculation should be like below

1Month + 1Month = 200 + 200 = 400 (booking period 43 day) // we can not divide month by day

43Day = 10 X 43 = 430

7Week = 7 X 50 = 350 

1Month + 13Day = 200 + 13 x 10 = 330

1Month + 1Week + 6D = 200 + 50 + 60 = 310 

1 Month + 2 Week = 200 + 100 = 300

Time slot may be change so it is not static, Then this booking price will 300. So How can I make algorithm for calculation ? Can someone help me please ?

0 likes
3 replies
Robstar's avatar

Just create a small isolated service I'd personally do something like:

$price = resolve(BookingPrice::class)
    ->onDay(1)
    ->onWeek(2)
    ->forMonth(12)
    ->calculate();

The onDay, onWeek methods simply set data within your class.

This way you can perform your calculations within your class as you would with an PHP class.

As the service is a simple PHP class it can easily unit tested.

Sti3bas's avatar

@phphupptechnologies the code might not look very clean, but it does it's job:

function calculatePrice($totalDays, $dayPrice = 10, $weekPrice = 50, $monthPrice = 200)
{
    $daysPerMonth = 30;
    $daysPerWeek = 7;

    $months = floor($totalDays / $daysPerMonth);
    $monthsPrice = $months * $monthPrice;
    $days = $totalDays - $months * $daysPerMonth;
    $weeks = $days / $daysPerWeek;

    return min([
        ceil($totalDays / $daysPerMonth) * $monthPrice,
        $totalDays * $dayPrice,
        ceil($totalDays / $daysPerWeek) * $weekPrice,
        $monthsPrice + $days * $dayPrice,
        $monthsPrice + floor($weeks) * $weekPrice + ($days - floor($weeks) * $daysPerWeek) * $dayPrice,
        $monthsPrice + ceil($weeks) * $weekPrice,
    ]);
}

dd(calculatePrice(43)); // 300
hupp's avatar
Level 11

@robstar @sti3bas thanks for Answer but timeslot is not fix. It was just example.It will like 2Days, 2Week,5day,3Month etc

Please or to participate in this conversation.