Jump to content

Show specific day of the week


Laz217

Recommended Posts

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

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.

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')); 

?>

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.

this should work better :P

 

<?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'));


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.