Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add minutes to to a date in laravel

Tags:

time

laravel

There are few ways that I can do this using PHP but I could not find a way to do that using laravel specific way.

I have a time that is coming from database in below format: Y:M:s

ex: 05:15:00

This is what I want to do:

add 30 minutes to that date, according to above example result should be: 05:45:00

Below is my current code and I want to add 30min to $endTime:

//get database value according to selected date
$allocatedDateQuery = DB::table('appointments')->where('date', $request->date)->get();

//loop throug every record to get time
foreach ($allocatedDateQuery as $value) {
    $time = $value->time;
    $endTime = $time;
}
like image 735
Blasanka Avatar asked Oct 17 '25 06:10

Blasanka


2 Answers

I just got a perfect solution from here.

Use Carbon extention to simply acheive that.

What you have to do is parse your time to Carbon object and then you can use addMinutes() to do that and then you can format() if you want:

foreach ($allocatedDateQuery as $value) {
    $time = Carbon::parse($value->time);
    $endTime = $time->addMinutes(30);
    $allocateValidateMessage .= Carbon::parse($value->time)->format('H:i') . ' - ' . $endTime->format('H:i') . '  ';
}
like image 108
Blasanka Avatar answered Oct 19 '25 19:10

Blasanka


Usually I use php's date, you can give this a try

Date("Y:M:s", strtotime("30 minutes", strtotime($value->time))

That is converting your time into a string, adding 30minutes to it and converting it to the date format of your desire

like image 23
abr Avatar answered Oct 19 '25 20:10

abr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!