note 68753 deleted from function.date by tularis

From: Date: Thu, 09 Feb 2012 21:04:38 +0000
Subject: note 68753 deleted from function.date by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186029@lists.php.net to get a copy of this message
Note Submitter: mwwaygoo AT hotmail DOT com ---- Number of weeks per month I was trying to do a monthly calendar and required the number of weeks in a month, running from Monday to Sunday. Since PHP doesn't have this in its date() parameters I had to calculate it in a roundabout manner. By subtracting the week numbers away from each other we SHOULD get the number of weeks, since it is calculated on Mondays. <?php $year = date("Y", $date); $month = date("m", $date); if( (isset($_GET['year'])) && (intval($_GET['year']) > 1582) ) { $year = intval($_GET['year']); } if( (isset($_GET['month'])) && (intval($_GET['month']) >= 1) && (intval($_GET['month']) <= 12) ) { $month = intval($_GET['month']); } $date = mktime(1, 1, 1, $month, date("d"), $year); $first_day_of_month = strtotime("-" . (date("d", $date)-1) . " days", $date); $last_day_of_month = strtotime("+" . (date("t", $first_day_of_month)-1) . " days", $first_day_of_month); $first_week_no = date("W", $first_day_of_month); $last_week_no = date("W", $last_day_of_month); if($last_week_no < $first_week_no) $last_week_no=date("W", strtotime("-1 week",$last_week_no)) + 1; $weeks_of_month = $last_week_no - $first_week_no + 1; ?> The check for weeknumber of the end of the month being smaller than the beginning of the month, is because of December. Where Monday 31st is actually in the first week of the following year. The +1 adjustment is for the number of weeks, inclusive. ie if January had five week, then 5-1=4, so we need to add an extra one to make it 5.

« previous php.notes (#186029) next »