number of days of the week per month

5

I need to check the number of the week and count how many days each week of the month has.

With this code, I know the number of the week in the month (not the week of the year):

function weekOfMonth($date) {
    // estract date parts
    list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));

    // current week, min 1
    $w = 1;

    // for each day since the start of the month
    for ($i = 1; $i <= $d; ++$i) {
        // if that day was a sunday and is not the first day of month
        if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0) {
            // increment current week
            ++$w;
        }
    }

    // now return
    return $w;
}

Now I want to count how many days each week number has (September 2017):

d  s  t  q  q  s  s
               01 02 = semana 1 -> 2 dias
03 04 05 06 07 08 09 = semana 2 -> 7 dias
10 11 12 13 14 15 16 = semana 3 -> 7 dias
17 18 19 20 21 22 23 = semana 4 -> 7 dias
24 25 26 27 28 29 30 = semana 5 -> 7 dias
    
asked by anonymous 12.09.2017 / 20:50

2 answers

1

function weekOfMonthWithWeek($date) 
{
    list($y, $m, $d) = explode('-', $date);
    $weeks = array();
    $s = date('w', strtotime("{$y}-{$m}-01"));
    $t = date('t', strtotime("{$y}-{$m}-01"));
    $c = 0;
    $i = 1;
    $weeks['month_year'] = "$y - $m";
    for($ini = date('Y-m-d', strtotime("{$y}-{$m}-01"));
        $ini <= date('Y-m-d', strtotime("{$y}-{$m}-{$t}"));
        $ini = date('Y-m-d', strtotime($ini." +1 days")))
    {
        if ($s == 6)
        {
            $s = 0;             
            $weeks['items'][] = array('week' => $i++, 'total_days' => $c+1);    
            $c = 0;
        }
        else
        {
            $c++;
            $s++;
        }

    }
    if ($s != 0) $weeks['items'][] = array('week' => $i++, 'total_days' => $c);


    return $weeks;
}


var_dump ( weekOfMonthWithWeek('2017-01-20') );

OnLine Example - Ideone

    
12.09.2017 / 22:42
1

Another solution is to make an array based on date('W', ...) and increment it, for example:

function DiasPorSemana($data)
{
    $tempo = strtotime('first day of this month', strtotime($data));
    $limite = date('t', $tempo);
    $resultado = array_fill((int)date('W',$tempo), 6, 0);

    for($i = 1; $i <= $limite; $i++){
        $resultado[(int)date('W', $tempo + (86400 * $i))] += 1; 
    }

    return array_values(array_filter($resultado));
}

Using:

var_dump(DiasPorSemana('2017-09-13'));

Will return:

array(5) {
  [0]=>
  int(2)
  [1]=>
  int(7)
  [2]=>
  int(7)
  [3]=>
  int(7)
  [4]=>
  int(7)
}

First we get the first day of the month, using:

$tempo = strtotime('first day of this month', strtotime($data));

Then we get the amount of days you have this month:

$limite = date('t', $tempo);

We have created an array with the 0 value for all possible weeks. The date('W',$tempo) will be the first week of the year of that month, so if it is 10 we will create an array containing 10 to 15 , all with the initial value of 0 :

$resultado = array_fill((int)date('W',$tempo), 6, 0);

We make a loop of all possible days and increment ( += 1 ) in the array where you have the key / index with the value equal to the respective week, so you can count the number of days that week.

for($i = 1; $i <= $limite; $i++){
    $resultado[(int)date('W', $tempo + (86400 * $i))] += 1; 
}

After returning the values, we remove from the weeks with 0 days and also "redo" the index of the array, starting from 0 :

return array_values(array_filter($resultado));
    
13.09.2017 / 12:52