Jump to content

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


Muiter

Recommended Posts

$isLastWeekInMonth = (date("m") != date("m",strtotime('+1 week')));
Link to comment
Share on other sites

What exactly do you mean by "week?" The last full week? What if the month ends on a Sunday? You would probably do something like what Hadien describes above.

Edited by davej
Link to comment
Share on other sites

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 comment
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 comment
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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...