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

DennisEilander's avatar

Add 1,5 hour (string) to Carbon object

Hi all,

Is it possible to do something like the example below?

$eventDate = Carbon::addDays(5); // just a Carbon date in the future
$duration = "1.5 uur"; // 1 hour 30 minutes
$duration = Carbon::parseFromLocale($duration, 'nl_NL'); // returns a carbon object with the correct time, but with the date of today


// TODO
$eventDate->addHours($duration);

I know the way the duration is saved makes it a lot harder to calculate this timestamp. So is there a way to add the duration string to the carbon object, if not, what is the suggestion to save this duration and how to convert it so it is readable by the user and it is saved correctly to de database.

===================================

(Extra information)

I have an event management tool where you simply can make a reservation to a party/event.

The event Model contains a duration field which contains a string with the duration of the event: 1 hour or 2.5 hour ( 2 hours 30 minutes). The user which fills in the duration has a dropdown select with these human readable times. The value is exact the same as the label.

Now when someone made a reservation for that event, they set the date and time (Carbon) when they want the event to be happen. It is a full Carbon date with time object.

Now I need to know that date and add the duration to it, so I know when the event will end.

0 likes
2 replies
MichalOravec's avatar
Level 75

@denniseilander Just add 90 minutes instead of 1,5 hour.

$eventDate->addMinutes(90);

And if you need you can convert hours to minutes and it will be work anytime.

$decimalHours = 2.5;

$hours = floor($decimalHours);

$mins = round(($decimalHours - $hours) * 60);

$timeInMinutes = ($hours * 60) + $mins;
2 likes

Please or to participate in this conversation.