Muiter Posted November 9, 2013 Share Posted November 9, 2013 How can I check if any given week is the last week in an month? Link to comment Share on other sites More sharing options...
Hadien Posted November 9, 2013 Share Posted November 9, 2013 $isLastWeekInMonth = (date("m") != date("m",strtotime('+1 week'))); Link to comment Share on other sites More sharing options...
davej Posted November 10, 2013 Share Posted November 10, 2013 (edited) 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 November 10, 2013 by davej Link to comment Share on other sites More sharing options...
Muiter Posted November 10, 2013 Author Share Posted November 10, 2013 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 More sharing options...
Don E Posted November 10, 2013 Share Posted November 10, 2013 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 More sharing options...
dsonesuk Posted November 10, 2013 Share Posted November 10, 2013 Would be fine if all months had 28 days, but because of last few days, it would eventually would give incorrect result, as you progress through the months. Link to comment Share on other sites More sharing options...
davej Posted November 11, 2013 Share Posted November 11, 2013 (edited) 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 November 11, 2013 by davej Link to comment Share on other sites More sharing options...
Hadien Posted November 11, 2013 Share Posted November 11, 2013 //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. 1 Link to comment Share on other sites More sharing options...
Don E Posted November 11, 2013 Share Posted November 11, 2013 (edited) 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 November 11, 2013 by Don E Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now