Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a script where I need to separate months by (Monday to Thursday) and (Friday to Sunday).

I am using a function to create an array of all dates in a range and then loop through that array to separate the days.

function createDateRangeArray($strDateFrom,$strDateTo){
    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

$date = "2013-07-01";
$end_date = "2013-07-31";
$date_range = createDateRangeArray($date, $end_date);

$final_array = array();
$temp_array = array();

foreach ($date_range as $value) {
    $temp_array[] = $value;
    $day_of_week = date("N", strtotime($value));

    if ($day_of_week == 4 || $day_of_week == 7) {
        $final_array[] = $temp_array;
        $temp_array = array();
    }

}

When I do a print_r($date_range) I get:

Array
(
    [0] => 2013-07-01
    [1] => 2013-07-02
    [2] => 2013-07-03
    [3] => 2013-07-04
    [4] => 2013-07-05
    [5] => 2013-07-06
    [6] => 2013-07-07
    [7] => 2013-07-08
    [8] => 2013-07-09
    [9] => 2013-07-10
    [10] => 2013-07-11
    [11] => 2013-07-12
    [12] => 2013-07-13
    [13] => 2013-07-14
    [14] => 2013-07-15
    [15] => 2013-07-16
    [16] => 2013-07-17
    [17] => 2013-07-18
    [18] => 2013-07-19
    [19] => 2013-07-20
    [20] => 2013-07-21
    [21] => 2013-07-22
    [22] => 2013-07-23
    [23] => 2013-07-24
    [24] => 2013-07-25
    [25] => 2013-07-26
    [26] => 2013-07-27
    [27] => 2013-07-28
    [28] => 2013-07-29
    [29] => 2013-07-30
    [30] => 2013-07-31
)

However when I run it through the foreach loop above it is grabbing everything except the last couple days of the month. If I run a print_r($final_array) I get this:

Array
(
    [0] => Array
        (
            [0] => 2013-07-01
            [1] => 2013-07-02
            [2] => 2013-07-03
            [3] => 2013-07-04
        )

    [1] => Array
        (
            [0] => 2013-07-05
            [1] => 2013-07-06
            [2] => 2013-07-07
        )

    [2] => Array
        (
            [0] => 2013-07-08
            [1] => 2013-07-09
            [2] => 2013-07-10
            [3] => 2013-07-11
        )

    [3] => Array
        (
            [0] => 2013-07-12
            [1] => 2013-07-13
            [2] => 2013-07-14
        )

    [4] => Array
        (
            [0] => 2013-07-15
            [1] => 2013-07-16
            [2] => 2013-07-17
            [3] => 2013-07-18
        )

    [5] => Array
        (
            [0] => 2013-07-19
            [1] => 2013-07-20
            [2] => 2013-07-21
        )

    [6] => Array
        (
            [0] => 2013-07-22
            [1] => 2013-07-23
            [2] => 2013-07-24
            [3] => 2013-07-25
        )

    [7] => Array
        (
            [0] => 2013-07-26
            [1] => 2013-07-27
            [2] => 2013-07-28
        )

)

What is even weirder is that when I change the date range to (2013-06-01 to 2013-06-30) or (2013-08-01 to 2013-08-31) it spits all the dates into $final_array the way that it is supposed to.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

My thought process is you're missing the final step of adding $temp_array back in, because you aren't quite getting to the the 4 or 7 mark. It works with other dates because the day of week sets up nicely.

Try confirming by adding the array (if not empty) after the foreach:

foreach ($date_range as $value) {
    $temp_array[] = $value;
    $day_of_week = date("N", strtotime($value));

    if ($day_of_week == 4 || $day_of_week == 7) {
        $final_array[] = $temp_array;
        $temp_array = array();
    }

}

if ( count( $temp_array ) > 0 ){ // anything leftover?
    $final_array[] = $temp_array; // append to $final_array
}
share|improve this answer
    
Haha I do not know how I missed that. It worked perfectly. I will mark this the accepted answer as soon as it will let me. –  Jeff Thomas Jul 11 '13 at 17:35
    
Sometimes just takes another pair of eyes ;) Happy to help! –  Set Sail Media Jul 11 '13 at 17:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.