number of days of the week per month

Asked

Viewed 477 times

5

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

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 plan 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

2 answers

1

Another solution is to make an array based on date('W', ...) and increase 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 took the first day of the month, using:

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

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

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

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

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

We loop every possible day (+= 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 existing in that week.

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

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

return array_values(array_filter($resultado));

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

  • 2

    Because they vote negative, is there something wrong, that I can fix? or is wrong?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.