Destramic Posted August 14, 2018 Share Posted August 14, 2018 (edited) hey guys i have a little problem and not sure of the best way to tackle it, if you could help please. i have 2 url which could be like so tasks/ricky-powell/week/14/08/2018 tasls/ricky-powell/day/14/08/2018 now if /day/14/08/2018 then thats a simple query. SELECT task FROM tasks WHERE scheduled_date = '2018-08-14' but...if users selects to view tasks by week (mon-fri) then it becomes a little bit complicated. 14/08/2018 is today which is a tuesday, and i would need to get the range from date mon - fri. would it be easier to: 1. ensure weekly url is a monday? 2.work out the date (mon - fri) view mysql/php 3. something else. thank you. Edited August 14, 2018 by Destramic Quote Link to comment Share on other sites More sharing options...
Barand Posted August 14, 2018 Share Posted August 14, 2018 try $date = '2018-08-14'; $dt = new DateTime($date); if ($dt->format('D') != 'Mon') { $dt->modify('last monday'); } echo $dt->format('Y-m-d'); //--> 2018-08-13 Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 14, 2018 Author Share Posted August 14, 2018 thank you for your quick reply. i would also need friday's date. ive been testing via sql, and think ive almost made a breakthrough, but i get an error. SELECT DATE_ADD(DATE('2018-08-14'), INTERVAL - WEEKDAY(DATE('2018-08-14')) DAY) AS 'week_start', DATE_ADD(week_start, INTERVAL 5 DAY) AS 'week_end' Quote Error Code: 1054. Unknown column 'week_start' in 'field list' Quote Link to comment Share on other sites More sharing options...
Barand Posted August 14, 2018 Share Posted August 14, 2018 Column aliases are applied on query output so week_start doesn't exist. Also the alias shouldn't be in quotes. Continuing from my previous code $monday = $dt->format('Y-m-d'); $friday = $dt->add(new DateInterval('P4D'))->format('Y-m-d'); or SELECT ... WHERE scheduled_date BETWEEN '$monday' AND '$monday' + INTERVAL 4 DAY Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 14, 2018 Author Share Posted August 14, 2018 thats brilliant...thank you for your help Barand ? much appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.