在我的 Laravel 项目中,我有一个包含以下列的 MySQL 数据库表:
| requested_at |
2018-02-03 12:00:00
2018-03-03 11:00:00
我想从该列中获取时间戳 (requested_at) 早于 60 天的所有项目:
$now = Carbon::now ();
$60days = DB::table('exampletable')->where('requested_at', '=', $now->subDays(60))->get();
对于我的以下代码,我需要从我的列 requested at
中格式化日期格式。
我只想得到这样的日期 2018-03-03
没有时间。
我试过一些 carbon 方法,但根本没用:
$format60 = Carbon::createFromFormat('Y-m-d', $60days);
最佳答案
看来你使用的是TIMESTAMP类型所以
->where('requested_at', '<=', now()->subDays(60)->startOfDay())->get();
如您所见,无需格式化 Carbon
实例。
分解一切:
now() // Laravel helper to get Carbon::now()
->subDays(60) // subtract 60 days
->startOfDay() // set time part of timestamp to start of the day
Note: add
requested_at
into$dates
array in your model for easier use
关于php - Laravel - 使用 carbon 格式化来自 Mysql 数据库的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50156431/