php Laravel $q->where() 日期之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24824624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel $q->where() between dates
提问by Jono20201
I am trying to get my cron to only get Projects
that are due to recur/renew in the next 7 days to send out reminder emails. I've just found out my logic doesn't quite work.
我试图让我的 cron 只得到Projects
那些在未来 7 天内重复/更新的发送提醒电子邮件。我刚刚发现我的逻辑不太行。
I currently have the query:
我目前有以下查询:
$projects = Project::where(function($q){
$q->where('recur_at', '>', date("Y-m-d H:i:s", time() - 604800));
$q->where('status', '<', 5);
$q->where('recur_cancelled', '=', 0);
});
However, I realized what I need to do is something like:
但是,我意识到我需要做的是:
Psudo SQL:
伪SQL:
SELECT * FROM projects WHERE recur_at > recur_at - '7 days' AND /* Other status + recurr_cancelled stuff) */
How would I do this in Laravel 4, and using the DATETIME datatype, I've only done this sort of thing using timestamps.
我将如何在 Laravel 4 中执行此操作,并且使用 DATETIME 数据类型,我只使用时间戳完成了此类操作。
Update:
更新:
Managed to solve this after using the following code, Stackoverflow also helps when you can pull bits of code and look at them out of context.
在使用以下代码后设法解决了这个问题,当您可以提取代码位并脱离上下文查看它们时,Stackoverflow 也有帮助。
$projects = Project::where(function($q){
$q->where(DB::raw('recur_at BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()'));
$q->where('status', '<', 5);
$q->where('recur_cancelled', '=', 0);
});
Updated Question:Is there better way to do this in Laravel/Eloquent?
更新的问题:在 Laravel/Eloquent 中是否有更好的方法来做到这一点?
Update 2:
更新 2:
The first resolution ended up not been right after further testing, I have now resolved and tested the following solution:
经过进一步测试,第一个解决方案最终不正确,我现在已经解决并测试了以下解决方案:
$projects = Project::where(function($q){
$q->where('recur_at', '<=', Carbon::now()->addWeek());
$q->where('recur_at', '!=', "0000-00-00 00:00:00");
$q->where('status', '<', 5);
$q->where('recur_cancelled', '=', 0);
});
回答by Tom
You can chain your where
s directly, without function(q)
. There's also a nice date handling package in laravel, called Carbon. So you could do something like:
您可以where
直接链接您的s,而无需function(q)
. Laravel 中还有一个不错的日期处理包,称为Carbon。所以你可以这样做:
$projects = Project::where('recur_at', '>', Carbon::now())
->where('recur_at', '<', Carbon::now()->addWeek())
->where('status', '<', 5)
->where('recur_cancelled', '=', 0)
->get();
Just make sure you require Carbon in composer and you're using Carbon namespace (use Carbon\Carbon;) and it should work.
只要确保你在作曲家中需要 Carbon 并且你正在使用 Carbon 命名空间(使用 Carbon\Carbon;),它应该可以工作。
EDIT: As Joel said, you could do:
编辑:正如乔尔所说,你可以这样做:
$projects = Project::whereBetween('recur_at', array(Carbon::now(), Carbon::now()->addWeek()))
->where('status', '<', 5)
->where('recur_cancelled', '=', 0)
->get();
回答by Edmund Sulzanok
Didn't wan to mess with carbon. So here's my solution
不想惹碳。所以这是我的解决方案
$start = new \DateTime('now');
$start->modify('first day of this month');
$end = new \DateTime('now');
$end->modify('last day of this month');
$new_releases = Game::whereBetween('release', array($start, $end))->get();
回答by Jaykumar Patil
@Tom : Instead of using 'now' or 'addWeek' if we provide date in following format, it does not give correct records
@Tom :如果我们按以下格式提供日期,则不会使用“now”或“addWeek”,它不会提供正确的记录
$projects = Project::whereBetween('recur_at', array(new DateTime('2015-10-16'), new DateTime('2015-10-23')))
->where('status', '<', 5)
->where('recur_cancelled', '=', 0)
->get();
it gives records having date form 2015-10-16 to less than 2015-10-23. If value of recur_at is 2015-10-23 00:00:00then only it showsthat record else if it is 2015-10-23 12:00:45then it is not shown.
它提供日期为 2015-10-16 到小于 2015-10-23 的记录。如果recur_at的值是2015年10月23日00:00:00那么只有这表明该记录否则,如果它是2015年10月23日12时00分45秒,然后就没有显示。
回答by Edwin M
Edited: Kindly note that whereBetween('date',$start_date,$end_date)
is inclusive of the first date.
编辑:请注意这whereBetween('date',$start_date,$end_date)
包括第一次约会。