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

RafaelMunoznl's avatar

Add Time Data Format to Date Time format

I have a carbon instance in the variable $startTime:

 Carbon @1574958600 {#781 ▼
  date: 2019-11-28 16:30:00.0 UTC (+00:00)
}

I have in the column "duration" of table OFFERS in the DB (time format) 30 minutes in this format: 00:30:00

Now I want to add those 30 minutes to the $startTime.

I get the data from DB:

$offer = Offer::select('duration')->where('id', $offerId)->first();

It works correctly. and I try to store that value into a variable $duration

$duration = $offer->duration;

and I get this error: Unexpected data found. Unexpected data found. Data missing

I supposed I have to format this stringto Carbon, so I did:

$duration = Appointment::setDateAttribute($offer->duration);

I got again the error: Unexpected data found. Unexpected data found. Data missing

And now I am lost.

I just wanted to make a simple addition:

$endTime = $startTime + $offer->duration;

But I am stock with this since hours.

Any idea what am I doing wrong?

Another though was:

Is it possible to make this operation in a query and add the 30 minutes 'on the fligh' while I get the duration? Somehow pass the variable $startTime to the query and do something like:

$duration= Offer::where('id', $offerId)
->whereHas('duration', function ($query) {
                $query->get('duration', 'and add the $startTime variable here'); // <- make the calculation here
            })
->select('duration as duration', 'endTime') // endTime would be the calculated data
->first();

Either one or another solution. But at the moment I am stock with this

0 likes
10 replies
bobbybouwmann's avatar
Level 88

I'm not sure if you can do it in the query, but you should be able to do it on the model itself. That will look like this

$offer = Offer::first();

$times = explode(':', $offer->duration);
$hours = $times[0];
$minutes = $times[1];
$seconds = $times[2];

$endTime = $offer->startTime
    ->copy()
    ->addHours($hours)
    ->addMinutes($minutes)
    ->addSeconds($seconds);

This should give you the correct result. AFAIK there is no way to do this with one method in Carbon. Anyway, you can find more examples of this in the docs: https://carbon.nesbot.com/docs/#api-addsub

Snapey's avatar

Are you sure you don't already have a mutator in your model like getDurationAttribute or you have added it (incorrectly) to the $dates array in your model

And when you get that error, pay attention to the file and line that errors

RafaelMunoznl's avatar

@snapey I have added "duration" to the date array.

    protected $dates = ['deleted_at', 'duration'];

It is a Time format data.

Is it not correct to add it to the $dates array?

Sinnbeck's avatar

So the duration is a date with a time? Like 2019-11-27 11:54:22 ?

Sinnbeck's avatar

That will try to format it as a date with time. Instead you could add a custom attribute handler

Something I this might work. (Be aware that it will use the current date as the date part in carbon, meaning subtracting it might be wrong. Look into the previous answers to know how to handle that properly)

public function getDurationAttribute( $value ) {
  return Carbon::parse($value);
}
Snapey's avatar

Adding the duration to the dates array will cause this error Unexpected data found. Unexpected data found. Data missing because eloquent is trying to parse it into a full datetime carbon object, which it can never be.

Do you have any control over duration? Is there anyway it can be converted to just total minutes?

If not then I think you need to explode the string into its parts.

$hms = explode(':',$offer->duration)

$endTime = $startTime->copy()->addHours($hms[0])->addMinutes($hms[1])->addSeconds($hms[2]);

1 like
Snapey's avatar

Then please mark Bobby's answer as best reply.

1 like

Please or to participate in this conversation.