gpt4 book ai didi

php - Laravel 获取用户今天和明天生日

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:40 27 4
gpt4 key购买 nike

我有这个查询来获取明天和今天生日的用户:

$birthday[Carbon::now()->format('Y-m-d')] = User::whereRaw("DATE_FORMAT(birthday, '%m-%d') = DATE_FORMAT(now(),'%m-%d')")
->orWhereRaw("DATE_FORMAT(birthday,'%m-%d') = '02-29' and DATE_FORMAT(birthday, '%m') = '02' AND
LAST_DAY(NOW()) = DATE(NOW())")
->selectRaw('first_name, last_name, email, birthday')
->get();

$birthday[Carbon::now()->addDay()->format('Y-m-d')] = User::whereRaw("DATE_FORMAT(birthday, '%m-%d') = DATE_FORMAT(NOW() + INTERVAL 1 DAY, '%m-%d')")
->orWhereRaw("DATE_FORMAT(birthday,'%m-%d') = '02-29' and DATE_FORMAT(birthday, '%m') = '02' AND
LAST_DAY(NOW()) = DATE(NOW())")
->selectRaw('first_name, last_name, email, birthday')
->get();

return $birthday;

查询将给我这个结果:

{
"2016-03-30": [
{
"first_name": "Harmon",
"last_name": "Olson",
"email": "GleasonSwaniawski.Jeanne@yahoo.com",
"birthday": "1990-03-30"
},
{
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@yahoo.com",
"birthday": "1990-03-30"
},
],
"2016-03-31": [
{
"first_name": "User10",
"last_name": null,
"email": "user10@user.com",
"birthday": "1990-03-31"
}
]
}

确定它工作正常并且结果正是我想要的,但我认为我的查询可以缩短(将查询合并为一个)以获得我想要的结果。

有什么建议可以缩短吗?

提前致谢!

最佳答案

获取今天的生日使用

User::whereMonth('birth_date', Carbon::now()->format('m'))
->whereDay('birth_date', Carbon::now()->format('d'))
->get();

还有明天的生日

User::whereMonth('birth_date', Carbon::now()->format('m'))
->whereDay('birth_date', Carbon::now()->addDay()->format('d'))
->get();

注意:- birth_date 列日期格式为 yyyy-mm-dd 数据类型为表中的date

关于php - Laravel 获取用户今天和明天生日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36304941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com