Jump to content

How can I check if any given week is the last week in an month?


Recommended Posts

Example.

I have a list of weeks

45 2013

46 2013

47 2013

48 2013

49 2013

50 2013

51 2013

52 2013

01 2014

02 2014

 

How can I check if week 45 is the last week of an month.

How can I check if week 46 is the last week of an month.

How can I check if week 47 is the last week of an month.

How can I check if week 48 is the last week of an month.

etc.

 

I also need to know wich week is the last in a quarter.

Link to post
Share on other sites

I'm not sure how reliable the below code is but I guess it's one way of doing it. Since there are basically 4 weeks in a month and a total of 52 weeks in a year, just take every fourth week, see if it's the current week and if so, that is your last week in a month.

        $islastWeek = false;	$weeks = array(4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52);		foreach($weeks as $lastWeekInMonth)	{		if(date('W') == $lastWeekInMonth) // date('W') current number of the week		{			$islastWeek = true;		}			}		if($islastWeek)	{		echo 'Yes, last week in month';	}	else	{		echo 'No, not last week in month';	}
Link to post
Share on other sites

How can I check if week 45 is the last week of an month.

 

If the month begins on any day except Sunday I'm guessing the week containing that day is both the last week of the month and also the first week of the month. If a month ends on a Saturday then I guess that week is only the last week of the month. If the month begins on a Sunday then I guess that week is only the first week of the month. But maybe your definition is different. Maybe the last week of the month must contain a Monday. You need to decide. Then you obviously need to study...

 

http://www.php.net/manual/en/ref.datetime.php

 

Using the function http://www.php.net/manual/en/function.date.php with the w character to obtain the numeric day-of-the-week (0-6).

 

You would probably have to loop through each month repeatedly using a function like http://www.php.net/manual/en/function.checkdate.php to figure out when each month ends.

Edited by davej
Link to post
Share on other sites
//parses "42 2013" to find 42nd week in the year 2013,// then sees if that week is the last week in the monthfunction isLastWeekInMonth($string){  list($week,$year)=split(" ",$string);  $start = "January 1st, $year";  $now = "$start +$week weeks";  $next = "$start +".($week+1)." weeks";  return Date('n',strtotime($now)) != Date('n',strtotime($next));  }function isLastWeekInQuarter($string){  list($week,$year)=split(" ",$string);  $start = "January 1st, $year";  $now = "$start +$week weeks";  $next = "$start +".($week+1)." weeks";  return (Date('n',strtotime($now)) != Date('n',strtotime($next)          && intval(Date('n',strtotime($next)))%3==1)  }

I don't have access to a php server atm so i can't even try to test this. not sure how strtotime would handle a combination of date and relative formats so I'm not sure it would work.

  • Like 1
Link to post
Share on other sites

Hadien,

 

Tested the isLastWeekInMonth function, seems to work. The only thing I had to change was the split function. It is deprecated. Instead used the explode function to split the string.

Edited by Don E
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...