Search code examples
mysqldatelaravel-5eloquentdate-range

Query gives empty result if I check whether the date 2019-01-12 falls with in a December to January date range


I'm creating an basic Zodiac signs app and I got stuck at this issue. Basically, I just want to check if today falls between a particular date range in the mysql table.

NOTE (IGNORE THE YEAR 2019. I just added that for querying convenience) This is my table: Zodia Signs Table

 $date = "2019-03-09"
    $zodiacSign =   DB::table('zodiac_signs')
        ->where(function ($query) use($date){
            $query->where('from_date', '<=', $date)
                  ->where('to_date', '>=', $date);
        })->get();

Every date except the date between 2019-12-22 and 2019-01-19 is working as expected. What am I doing wrong here?

+-----+--------------+-------------------+-------------+-------------+-------------+----------------------+----------------------+----------------------+------------+
| id  |    name      |      symbol       |    icon     | from_date   |  to_date    |     description      |     created_at       |     updated_at       | deleted_at |
+-----+--------------+-------------------+-------------+-------------+-------------+----------------------+----------------------+----------------------+------------+
|  2  | Aries        | The Ram           | NULL        | 2019-03-21  | 2019-04-19  |                      | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
|  3  | Taurus       | The Bull          | NULL        | 2019-04-20  | 2019-05-20  |                      | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
|  4  | Gemini       | The Twins         | NULL        | 2019-05-21  | 2019-06-20  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
|  5  | Cancer       | The Crab          | NULL        | 2019-06-21  | 2019-07-22  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
|  6  | Leo          | The Lion          | NULL        | 2019-07-23  | 2019-08-22  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
|  7  | Virgo        | The Virgin        | NULL        | 2019-08-23  | 2019-09-22  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
|  8  | Libra        | The Scales        | NULL        | 2019-09-23  | 2019-10-22  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
|  9  | Scorpio      | The Scorpion      | NULL        | 2019-10-23  | 2019-11-21  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
| 10  | Sagittarius  | The Archer        | NULL        | 2019-11-22  | 2019-12-21  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
| 11  | Capricorn    | The Goat          | NULL        | 2019-12-22  | 2019-01-19  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
| 12  | Aquarius     | The Water Bearer  | NULL        | 2019-01-20  | 2019-02-18  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
| 13  | Pisces       | The Fishes        | NULL        | 2019-02-19  | 2019-03-20  | NULL                 | 2019-07-17 03:09:07  | 2019-07-17 03:09:07  | NULL       |
+-----+--------------+-------------------+-------------+-------------+-------------+----------------------+----------------------+----------------------+------------+

Solution

  • The quick and lazy solution is to add Capricorn twice. Once to be part of the start of the year (starts in 2018) and once for the end of the year (ends in 2020). If it's not desirable to have zodiacs appear twice, you will need to update your query to special-case the zodiac that crosses the year line.