fork download
  1. <?php
  2.  
  3. function weekOfMonthWithWeek($date)
  4. {
  5. list($y, $m, $d) = explode('-', $date);
  6. $weeks = array();
  7. $s = date('w', strtotime("{$y}-{$m}-01")); // 2
  8. $t = date('t', strtotime("{$y}-{$m}-01")); // 2
  9. $c = 0;
  10. $i = 1;
  11. $weeks['month_year'] = "$y - $m";
  12. for($ini = date('Y-m-d', strtotime("{$y}-{$m}-01"));
  13. $ini <= date('Y-m-d', strtotime("{$y}-{$m}-{$t}"));
  14. $ini = date('Y-m-d', strtotime($ini." +1 days")))
  15. {
  16. if ($s == 6)
  17. {
  18. $s = 0;
  19. $weeks['items'][] = array('week' => $i++, 'total_days' => $c+1);
  20. $c = 0;
  21. }
  22. else
  23. {
  24. $c++;
  25. $s++;
  26. }
  27.  
  28. }
  29. if ($s != 0) $weeks['items'][] = array('week' => $i++, 'total_days' => $c);
  30.  
  31.  
  32. return $weeks;
  33. }
  34.  
  35.  
  36. var_dump ( weekOfMonthWithWeek('2017-01-20') );
Success #stdin #stdout 0.01s 83904KB
stdin
Standard input is empty
stdout
array(2) {
  ["month_year"]=>
  string(9) "2017 - 01"
  ["items"]=>
  array(5) {
    [0]=>
    array(2) {
      ["week"]=>
      int(1)
      ["total_days"]=>
      int(7)
    }
    [1]=>
    array(2) {
      ["week"]=>
      int(2)
      ["total_days"]=>
      int(7)
    }
    [2]=>
    array(2) {
      ["week"]=>
      int(3)
      ["total_days"]=>
      int(7)
    }
    [3]=>
    array(2) {
      ["week"]=>
      int(4)
      ["total_days"]=>
      int(7)
    }
    [4]=>
    array(2) {
      ["week"]=>
      int(5)
      ["total_days"]=>
      int(3)
    }
  }
}