Laravel eloquent where date is equal or greater than DateTime

98,564

Solution 1

Sounds like you need to use >=, for example:

->whereDate('date', '>=', Carbon::now('Europe/Stockholm'))

Solution 2

Here you can use this:

->where('date', '>=', date('Y-m-d'))

Solution 3

Using whereDate will compare the date only and ignore the time. So your solution will give the records that are at least dating one day later and the records that are in the same day but with later hours will not be included.

If you use >= as suggested in other answers, you will get records starting from the current date and those ones who are even before the determined hour.

One solution for this is comparing using MySQL functions in whereRaw. In your code the condition for the date will be like this:

-> whereRaw("date > STR_TO_DATE(?, '%Y-%m-%d %H:%i:%s')" , Carbon::now('Europe/Stockholm')->format('Y-m-d H:i'));

In the code, I changed the format of Carbon date to match a specific format where you can use whatever format you want and put the same format in the STR_TO_DATE function.

Solution 4

For Laravel's TestCases:

$this->assertDatabaseHas('my_table', [
    'name' => $name,
    [ 'updated_at', '>=', $updatedAt ] // $updatedAt is a Carbon object
]);
Share:
98,564
Kaizokupuffball
Author by

Kaizokupuffball

Updated on January 07, 2022

Comments

  • Kaizokupuffball
    Kaizokupuffball over 2 years

    I'm trying to fetch relational data from a model where the date column is higher or equal to the current time.

    The date column is formated as this: Y-m-d H:i:s

    What I'm trying to do is to grab all rows where the Y-m-d H:i:s is in the future.

    Example: lets assume the date is 2017-06-01 and the time is 09:00:00 Then i would like got all rows where the date is in the future, and the time is in the future.

    Currently my code looks like this, and it's almost working but it doesn't grab the rows where the date is the current day.

    public function customerCardFollowups() {
        return $this -> hasMany('App\CustomerCardFollowup', 'user_id') -> whereDate('date', '>', Carbon::now('Europe/Stockholm')) -> orderBy('date', 'asc') -> take(20);
    }
    

    What am I doing wrong? Thanks in advance.

  • Kaizokupuffball
    Kaizokupuffball almost 7 years
    I tried that too, but it doesn't seem to go by the time at the end. Example if the date is right, but the time past what the time is at the date column, it should not be fetched. Only dates that has not passed (including time) should be fetched.
  • Christian
    Christian over 5 years
    @Kaizokupuffball Did u fix it? I have the same problem and replaced it with only where intead of whereDate
  • benjaminhull
    benjaminhull almost 4 years
    This will only compare the date (2020-08-07) but ignore the time component (12:15:09).
  • Abdul Razak Zakieh
    Abdul Razak Zakieh over 2 years
    This solution will ignore time and in the asked question it will return those records timed before 2017-06-01 09:00 in the same day. You can check my answer here to include time in the comparison: stackoverflow.com/a/69687327/4332391
  • dhruvin prajapati
    dhruvin prajapati over 2 years
    it's work for me, thank you
  • Abdul Razak Zakieh
    Abdul Razak Zakieh over 2 years
    Happy to hear that.