zwl1619 zwl1619 - 3 years ago 149
PHP Question

Laravel : How to add days to datetime field?

How to add days to datetime field in Laravel?

For example,

there is a

updated_at
field in
articles
table:

$article = Article::find(1);
$updated_at=$article->updated_at;


I want to add 30 days to
updated_at
field.

In
Carbon
, it could be done like this:

$expired_at=Carbon::now()->addDays(30);


But how to do it in above example?

Answer Source

Since updated_at and created_at fields are automatically cast to an instance of Carbon you can just do:

$article = Article::find(1);
$article->updated_at->addDays(30);
// $article->save(); If you want to save it

Or if you want it in a separate variable:

$article = Article::find(1);
$updated_at = $article->updated_at;
$updated_at->addDays(30); // updated_at now has 30 days added to it
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download