Laravel:如何在日期时间字段中增加天数? [英] Laravel : How to add days to datetime field?

查看:130
本文介绍了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

这篇关于Laravel:如何在日期时间字段中增加天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆