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

老子叫甜甜 提交于 2019-12-23 19:49:32

问题


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.


回答1:


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);



回答2:


you can add something like that:

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


来源:https://stackoverflow.com/questions/48014766/how-can-i-add-date-with-another-date-in-carbon-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!