Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- echo (getWeekOfMonth( time() ) . "<BR>"); //today
- echo (getWeekOfMonth('2015-10-13') . "<BR>");
- /**
- * @param $time Either in timestamp or a date format that will be accepted by strtotime()
- * @return day of month (1-based), or bool FALSE on failure
- */
- function getWeekOfMonth($time)
- {
- // If we have something that is not in timestamp format,
- // convert it, if we can
- if (($tmp = strtotime($time)) !== false) {
- $time = $tmp;
- }
- // Get the day of the month, in numerical format
- $dayOfMonth = date('j', $time);
- // Get the timestamp for the first day of the month
- $firstDayOfMonthTime = $time - ($dayOfMonth - 1) * 60 * 60 * 24;
- // Get the days until week one ends
- $daysUntilWeekOneEnd = 6 - date('w', $firstDayOfMonthTime);
- // Create an array showing the numerical day of hte month when the week ends
- $weekEnd = array();
- $weekEnd[] = $daysUntilWeekOneEnd + 1;
- $weekEnd[] = $daysUntilWeekOneEnd + 8;
- $weekEnd[] = $daysUntilWeekOneEnd + 15;
- $weekEnd[] = $daysUntilWeekOneEnd + 22;
- $weekEnd[] = $daysUntilWeekOneEnd + 29;
- // Loop through the week end numbers until we find the week we are on
- foreach ($weekEnd as $index => $end) {
- if ($dayOfMonth <= $end) {
- // Then return that week plus one, because the array is 0-indexed
- return ($index+1);
- }
- }
- // If we got this far something went wrong. Return false
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement