{"id":18,"date":"2021-03-18T04:35:13","date_gmt":"2021-03-18T08:35:13","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/?p=18"},"modified":"2021-03-18T09:20:40","modified_gmt":"2021-03-18T13:20:40","slug":"querying-dates-and-date-parts-in-laravel","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2021\/03\/18\/querying-dates-and-date-parts-in-laravel\/","title":{"rendered":"Querying Dates and Date Parts in Laravel"},"content":{"rendered":"<p>The World War II timeline application allows users to query historical events by a specific date, by month\/day combination, by a year, or they can leave everything blank and the system will show them events that had happened &#8220;on this day&#8221;.  Here&#8217;s an example for <a href=\"https:\/\/ww2db.com\/event\/today\/3\/18\/1944\" target=\"_ww2db\" rel=\"noopener\">18 March 1944<\/a>.  To achieve this, in SQL speak, it would be something like the following.<\/p>\n<pre class=\"code\">\r\n-- By specific date\r\nselect * from my_table where my_date=:yyyymmdd;\r\n\r\n-- By year\r\nselect * from my_table where date_format(my_date, '%Y')=:yyyy;\r\n<\/pre>\n<p>In Laravel&#8217;s This can be achieved with several Laravel Eloquent&#8217;s built in functions: whereDate(), whereYear(), whereMonth(), and whereDay().  Below are some examples from WW2DB&#8217;s TimelineController.<\/p>\n<pre class=\"code\">\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse Illuminate\\Http\\Request;\r\nuse App\\Timeline;\r\n\r\nclass TimelineController extends Controller {\r\n\r\n\t\/* ... Unrelated code omitted from this article *\/\r\n\r\n\tpublic function getList(Request $request) {\r\n\t\t\/* ... Unrelated code omitted from this article *\/\r\n\r\n\t\t$mm = $request->query('mm');\r\n\t\t$dd = $request->query('dd');\r\n\t\t$yyyy = $request->query('yyyy');\r\n\t\t\r\n\t\t$timeline = Timeline::orderBy('my_date');\r\n\t\t\r\n\t\t\/\/ If querying by specific date, use whereDate()\r\n\t\t$timeline = $timeline->whereDate('my_date', date($yyyy.\"-\".$mm.\"-\".$dd));\r\n\t\t\r\n\t\t\/\/ If querying by month\/day, use whereMonth() + whereDay()\r\n\t\t$timeline = $timeline->whereMonth('my_date', $mm)\r\n\t\t\t->whereDay('my_date', $dd);\r\n\r\n\t\t\/\/ If querying by year, use whereYear()\r\n\t\t$timeline = $timeline->whereYear('my_date', $yyyy);\r\n\t\t\r\n\t\t$timeline = $timeline->get();\r\n\t\t\r\n\t\treturn view('timeline.my_view', compact('timeline'));\r\n\t}\r\n\r\n\t\/* ... Unrelated code omitted from this article *\/\r\n}\r\n<\/pre>\n<p>Or, simplified whereMonth() + whereDay() example to get data for 18 March:<\/p>\n<pre class=\"code\">\r\n$mm = \"03\";\r\n$dd = \"18\";\r\n$timeline = Timeline::orderBy('my_date');\t\t\r\n$timeline = $timeline->whereMonth('my_date', $mm)\r\n\t->whereDay('my_date', $dd);\r\n$timeline = $timeline->get();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This article demonstrates how to query for dates and date parts using Laravel Eloquent.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":9,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":393,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions\/393"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}