1
votes

I'm trying to assign weeks from 1 - 5 for days in a month i.e

  Day 1 - 7   = Week 1
  Day 8 - 14  = Week 2
  Day 15 - 21 = Week 3
  Day 22 - 28 = Week 4
  Day 23 - 31 = Week 5

for all the month in a normal year and leap year.

My question is: for a month like September 2012 (Sunday - Saturday) where Sept 1 is the last day for Week 35, and Sept 30 is the beginning of Week 40, thereby not making the weeks unique.

Will my week numbering be always true ?

I'm getting days of the month like this. Also this code was used to get Weeks from 1 - 53. But I only want week 1 - 4/5 for individual months.

$month array('january'.........'december');
$year = array('2011',........'2099');
for($i=0; $i<count($year); $i++){
    for($j=0; $j<count($month); $j++){
        $stamp = strtotime("last Monday of $month[$j] $year[$i]");
        $date = date("d-M-Y", $stamp); 
        $newdate = explode("-", $date); 
        echo "Day - $date[0]". "<br>";
        echo "Month - $date[1]". "<br>";
        echo "Year - $date[2]". "<br><br>";
1
Could you show the code that you're using to number the days?Ja͢ck
I'd go with (int) (date('d', $date) / 7) + 1. Depends what you need. Please clarify what output you need.Mikulas Dite
What @think123 is trying to say is that you should go through your previous questions and accept an answer where applicable, as outlined in the faq: stackoverflow.com/faq#howtoaskJa͢ck
@Jack Please see update.ilp
@ilp If I understood it correctly, I suppose that the solution I've posted in a previous comment might be what you need, isn't it?Mikulas Dite

1 Answers

3
votes

You might use this snippet

<?php
function weekInMonth($date) {
return (int) ((date('d', $date) - 1) / 7) + 1;
}    

Returns:

weekInMonth(strToTime('2012/09/01')); // 1
weekInMonth(strToTime('2012/09/14')); // 2
weekInMonth(strToTime('2012/09/15')); // 3
weekInMonth(strToTime('2012/09/21')); // 3
weekInMonth(strToTime('2012/09/22')); // 4
weekInMonth(strToTime('2012/09/28')); // 4
weekInMonth(strToTime('2012/09/29')); // 5

Yet again, I'm not quite sure that it works for your use case.