soycharliente Posted February 18, 2008 Share Posted February 18, 2008 I found this code online. <?php function findDays($startDate, $endDate, $days) { $endDate = strtotime($endDate); $days = explode(',', $days); $dates = array(); foreach ($days as $day) { $newDate = $startDate; switch ($day) { case 'Su': $day = 'Sun'; break; case 'M': $day = 'Mon'; break; case 'T': $day = 'Tue'; break; case 'W': $day = 'Wed'; break; case 'Th': $day = 'Thur'; break; case 'F'; $day = 'Fri'; break; case 'S': $day = 'Sat'; break; } while (($date = strtotime($newDate)) <= $endDate) { $dates[] = date("Y-m-d", $date) . "\n"; $newDate = date("Y-m-d", $date) . " next " . $day; } } sort(array_unique($dates)); return $dates; } ?> Obviously this code is very inefficient. It runs through every day. Can someone suggest a better way to find all the dates (the ones I want to check against all reside in a database) that are a specific day of the week? I don't want code, but some steps to follow. I can't even begin to think about how to attack this. If this doesn't work out, I'm toying with the idea of just adding a db field that stores the day of the week. Link to comment https://forums.phpfreaks.com/topic/91703-solved-finding-all-the-mondays/ Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 SELECT * FROM table WHERE DAYNAME(field) = 'Monday' Link to comment https://forums.phpfreaks.com/topic/91703-solved-finding-all-the-mondays/#findComment-469661 Share on other sites More sharing options...
soycharliente Posted February 18, 2008 Author Share Posted February 18, 2008 I ran the query in the SQL window inside phpMyAdmin and it returned 0 results. Is there something wrong with this query? SELECT * FROM lunches WHERE DAYNAME('thedate')='Monday' Link to comment https://forums.phpfreaks.com/topic/91703-solved-finding-all-the-mondays/#findComment-469675 Share on other sites More sharing options...
soycharliente Posted February 18, 2008 Author Share Posted February 18, 2008 Nevermind. I typed his answer in wrong. Link to comment https://forums.phpfreaks.com/topic/91703-solved-finding-all-the-mondays/#findComment-469677 Share on other sites More sharing options...
soycharliente Posted February 18, 2008 Author Share Posted February 18, 2008 SOLVED Link to comment https://forums.phpfreaks.com/topic/91703-solved-finding-all-the-mondays/#findComment-469704 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.