Laravel $q->where() between dates

前端 未结 4 1045
死守一世寂寞
死守一世寂寞 2020-11-30 02:49

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\

相关标签:
4条回答
  • 2020-11-30 03:31

    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();
    
    0 讨论(0)
  • 2020-11-30 03:33

    You can chain your wheres directly, without function(q). There's also a nice date handling package in laravel, called Carbon. So you could do something like:

    $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.

    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();
    
    0 讨论(0)
  • 2020-11-30 03:45

    @Tom : Instead of using 'now' or 'addWeek' if we provide date in following format, it does not give correct records

    $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:00 then only it shows that record else if it is 2015-10-23 12:00:45 then it is not shown.

    0 讨论(0)
  • 2020-11-30 03:45

    Edited: Kindly note that
    whereBetween('date',$start_date,$end_date)
    is inclusive of the first date.

    0 讨论(0)
提交回复
热议问题