Laravel:如何在日期时间字段中增加天数?

Laravel:如何在日期时间字段中增加天数?

问题描述:

如何在Laravel的日期时间字段中添加天数?

How to add days to datetime field in Laravel?

例如,
articles 表中有一个 updated_at 字段:

For example,
there is a updated_at field in articles table:

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

我想在 updated_at 字段中添加30天.

I want to add 30 days to updated_at field.

Carbon 中,可以这样完成:

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

但是在上面的示例中该怎么做?

But how to do it in above example?

由于 updated_at created_at 字段自动转换为 Carbon 的实例>您可以这样做:

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