skip to Main Content

I need to compare today to the date twenty business days after from created_at. I have afterTwentyBusinessDays function. Is it possiple to use it with where in laravel?

->where(afterTwentyBusinessDays($created_at), today())

3

Answers


  1. You have to get a date that was twenty days ago and then set in where condition

    ->whereDate('created_at', date('Y-m-d', strtotime('-20 days')));
    
    Login or Signup to reply.
  2. I presume afterTwentyBusinessDays($created_at) return carbon date time object.

    Try $query->whereDate('created_at', '>', afterTwentyBusinessDays($created_at))

    Login or Signup to reply.
  3. The most efficient way is to use carbon

    now()->diffInDays('2023-1-23 07:06:31')     // 19
    

    now() will give you carbon instance alongwith UTC date and time. You can compare any dates, years, months, months, weeks even seconds.

    Explore Carbon docs or visit detailed Carbon methods and their uses

    https://www.digitalocean.com/community/tutorials/easier-datetime-in-laravel-and-php-with-carbon

    Why you are not using Eloquent Model‘s own predefined methods to compare date and times like whereDate(), whereDay(), whereMonth() or wheryear() there are many.

    Also you can play with these days, remember to parse in your timezone and format

    User::whereDay('created_at', '>', '2023-1-23 07:06:31')->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search