Laz217 Posted December 2, 2009 Share Posted December 2, 2009 I have been racking my brain trying to get this function to work properly and it is doing so for everything but Sundays. What I am trying to do is display all dates within a specified date range that fall within a selected day of the week (ie. Monday,etc). This function serves multiple purposes where it can be used for just one day of the week, for all weekdays, or weekends. Everything works but Sundays do not show up. Can anyone please help or suggest any other function that will accomplish what I am trying to do? Here is the code: function getDOW($moBeg, $dayBeg, $moEnd, $dayEnd, $year, $dayWeek) { $week = 0; $dayBeg = $dayBeg - 1; $DOW = array(); $i = 1; //Get first date of specified day from the beginning of the date range if ($dayWeek == 0){ $week = date("w", mktime(12, 0, 0, $moBeg, $dayBeg, $year)); } else { while ($week != $dayWeek) { //Specify the day of the week (ie. Monday = 1) $dayBeg++; $week = date("w", mktime(12, 0, 0, $moBeg, $dayBeg, $year)); } } array_push($DOW,date("Ymd", mktime(12, 0, 0, $moBeg, $dayBeg, $year))); $weekBeg = date("W", mktime(12, 0, 0, $moBeg, $dayBeg, $year)); $weekEnd = date("W", mktime(12, 0, 0, $moEnd, $dayEnd, $year)); while ($weekNum < $weekEnd) { $addWeek = strtotime(date("r", mktime(12, 0, 0, $moBeg, $dayBeg, $year)) . "+" . $i . " week"); $addWeekConv = date("Ymd", $addWeek); $weekNum = date("W", $addWeek); if (substr($addWeekConv,0,4) == $year && substr($addWeekConv,4,2) <= $moEnd){ //Conditional fixes problem where an extra date from next month or year is added array_push($DOW,$addWeekConv); $i++; } } return $DOW; } Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/ Share on other sites More sharing options...
waynew Posted December 2, 2009 Share Posted December 2, 2009 I was a little confused about what you're wanting to do, so I'm going to assume that others who read this topic were also a little confused about what you are trying to do. Please give us a clearer explanation about what you want to achieve. Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/#findComment-970134 Share on other sites More sharing options...
Laz217 Posted December 2, 2009 Author Share Posted December 2, 2009 Sorry for the confusion. Basically when calling this function I enter a date range in the form: Beginning month ($moBeg), beginning day ($dayBeg), ending month ($moEnd), ending day($dayEnd), year ($year), and the day of the week ($dayWeek) as a numerical value (ie. Sunday = 0, Monday = 1, etc.). So when I enter, for example: getDOW(12,1,12,31,2009,1) It will put in an array all the mondays during Dec 1st and Dec 31st, 2009. This works perfectly, but for some reason Sundays do not work. Does this make sense? I appreciate your time and help. Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/#findComment-970147 Share on other sites More sharing options...
rajivgonsalves Posted December 2, 2009 Share Posted December 2, 2009 I think that is because the day of week check is there 0 that is Sunday anyways you can use this function I wrote hope its helpful <?php function getDays($start_date, $end_date, $day_of_week) { $dates = array(); while ($start_date != $end_date) { if (date("l", strtotime($start_date)) == $day_of_week) { $dates[] = $start_date; } $start_date = date('Y-m-d', strtotime("+1 day", strtotime($start_date))); } return $dates; } print_r(getDays('2009-12-01', '2009-12-31', 'Sunday')); ?> Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/#findComment-970154 Share on other sites More sharing options...
Laz217 Posted December 3, 2009 Author Share Posted December 3, 2009 Brilliant! That code will work perfectly. Thank you so much for all your help, Rajiv. I sincerely appreciate it. Laz Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/#findComment-970230 Share on other sites More sharing options...
Laz217 Posted December 3, 2009 Author Share Posted December 3, 2009 Hmmm.. Looks like there is one little bug in the code you provided, Ratjiv. If you use the following call to the function: getDays('2009-12-01', '2009-12-31', 'Thursday'); It displays: Array ( [0] => 2009-12-03 [1] => 2009-12-10 [2] => 2009-12-17 [3] => 2009-12-24 ) It appears that the last day of the month (30 or 31st) does not show up in the array. Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/#findComment-970532 Share on other sites More sharing options...
rajivgonsalves Posted December 3, 2009 Share Posted December 3, 2009 this should work better <?php function getDays($start_date, $end_date, $day_of_week) { $dates = array(); while (strtotime($start_date) <= strtotime($end_date)) { if (date("l", strtotime($start_date)) == $day_of_week) { $dates[] = $start_date; } $start_date = date('Y-m-d', strtotime("+1 day", strtotime($start_date))); } return $dates; } print_r(getDays('2009-12-01', '2009-12-31', 'Thursday')); ?> Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/#findComment-970535 Share on other sites More sharing options...
Laz217 Posted December 3, 2009 Author Share Posted December 3, 2009 Wow! That was quick. Looks to be working perfectly. Thank you again, Rajiv. That was very kind of you for being so helpful. I appreciate it. Link to comment https://forums.phpfreaks.com/topic/183795-show-specific-day-of-the-week/#findComment-970549 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.