46

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?

starball
  • 20,030
  • 7
  • 43
  • 238
Kaizokupuffball
  • 2,703
  • 8
  • 38
  • 58

4 Answers4

61

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

->whereDate('date', '>=', Carbon::now('Europe/Stockholm'))
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 3
    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. – Kaizokupuffball May 30 '17 at 15:37
  • 2
    @Kaizokupuffball Did u fix it? I have the same problem and replaced it with only `where` intead of `whereDate` – Christian Sep 30 '18 at 14:09
  • 2
    This will only compare the date (2020-08-07) but ignore the time component (12:15:09). – benjaminhull Aug 07 '20 at 20:33
  • 2
    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: https://stackoverflow.com/a/69687327/4332391 – Abdul Razak Zakieh Oct 23 '21 at 10:41
42

Here you can use this:

->where('date', '>=', date('Y-m-d'))
Bassil Qureshi
  • 531
  • 4
  • 4
9

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.

2

For Laravel's TestCases:

$this->assertDatabaseHas('my_table', [
    'name' => $name,
    [ 'updated_at', '>=', $updatedAt ] // $updatedAt is a Carbon object
]);
Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46