3

Hi I am collecting the month and the year from a form and want to generate an array of the number of dates on that particular month seperated by weeks assuming sunday as the beginning of week.

For example, if the input is month=January and year=2018, the desired result is

$array_of_dates =Array ( [Week 1] => '01/01/2018',.....,'06/01/2018'
                         [Week 2] => '07/01/2018',.....,'13/01/2018' 
                         [Week 3] => '14/01/2018',.....,'20/01/2018'
                         [Week 4] => '21/01/2018',.....,'27/01/2018' 
                         [Week 5] => '28/01/2018',.....,'31/01/2018' ) 

I tried this

function weeks_in_month($month, $year) {
   $start = mktime(0, 0, 0, $month, 1, $year);
   $end = mktime(0, 0, 0, $month, date('t', $start), $year);
   $start_week = date('W', $start);
   $end_week = date('W', $end);

if ($end_week < $start_week) { // Month wraps
  return ((52 + $end_week) - $start_week) + 1;
}

return ($end_week - $start_week) + 1;
}

But this only gives me the number of weeks for that month.

Red Bottle
  • 2,839
  • 4
  • 22
  • 59
  • I found this topics, may be it helps you. [stackoverflow](https://stackoverflow.com/questions/4861384/php-get-start-and-end-date-of-a-week-by-weeknumber) – vodasan Jan 19 '18 at 07:09

1 Answers1

3

I would use the DateTime class for convenience methods such as add to increment days, and format to get the total number of days in a month as well as the respective day of the week.

Demo: https://3v4l.org/IKGMG

function weeks_in_month($month, $year)
{
    $dates = [];

    $week = 1;
    $date = new DateTime("$year-$month-01");
    $days = (int)$date->format('t'); // total number of days in the month

    $oneDay = new DateInterval('P1D');

    for ($day = 1; $day <= $days; $day++) {
        $dates["Week $week"] []= $date->format('d/m/Y');

        $dayOfWeek = $date->format('l');
        if ($dayOfWeek === 'Saturday') {
            $week++;
        }

        $date->add($oneDay);
    }

    return $dates;
}

print_r(weeks_in_month(1, 2018));
Array
(
    [Week 1] => Array
        (
            [0] => 01/01/2018
            [1] => 02/01/2018
            [2] => 03/01/2018
            [3] => 04/01/2018
            [4] => 05/01/2018
            [5] => 06/01/2018
        )

    [Week 2] => Array
        (
            [0] => 07/01/2018
            [1] => 08/01/2018
            [2] => 09/01/2018
            [3] => 10/01/2018
            [4] => 11/01/2018
            [5] => 12/01/2018
            [6] => 13/01/2018
        )

    [Week 3] => Array
        (
            [0] => 14/01/2018
            [1] => 15/01/2018
            [2] => 16/01/2018
            [3] => 17/01/2018
            [4] => 18/01/2018
            [5] => 19/01/2018
            [6] => 20/01/2018
        )

    [Week 4] => Array
        (
            [0] => 21/01/2018
            [1] => 22/01/2018
            [2] => 23/01/2018
            [3] => 24/01/2018
            [4] => 25/01/2018
            [5] => 26/01/2018
            [6] => 27/01/2018
        )

    [Week 5] => Array
        (
            [0] => 28/01/2018
            [1] => 29/01/2018
            [2] => 30/01/2018
            [3] => 31/01/2018
        )

)
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167