Hkm Sadek
Hkm Sadek

Reputation: 3209

How can I add date with another date in carbon laravel?

In my laravel project, I want to increase the date time with the previous date time.

Here is my code

$expire=$order[0]->expire_date;
$new=Carbon::now()->addMonths(6);

The results of these two lines are

2018-01-28 21:23:56
2018-06-28 21:43:29

What I want to do is sum the both dates.

$expire+$new 

How can I achieve this? Any idea or thought? Thanks.

Edit with more clear view

I want to find the remaining days of the expire date from now and then want to add this remaining date with the new date. Hope it clears.

Upvotes: 4

Views: 10053

Answers (2)

Stojan Kukrika
Stojan Kukrika

Reputation: 312

you can add something like that:

$new=Carbon::createFromTimestamp($order[0]->expire_date->timestamp)->addMonths(6);

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

If you want to add six months to order expiration date, just do this:

$new = $order[0]->expire_date->addMonths(6);

This will work since $order[0]->expire_date is a Carbon instance.

If it's not, just parse it first:

$new = Carbon::parse($order[0]->expire_date)->addMonths(6);

Upvotes: 7

Related Questions