note 68753 added to function.date
| From: | mwwaygooAThotmailDOTcom at osu1 dot php dot net | Date: | Wed, 09 Aug 2006 15:11:13 +0000 |
| Subject: | note 68753 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115729@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 83.138.144.80
Probable Submitter: 80.1.224.4 (proxied: 82.11.219.158)
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/68753
Del: integrated -- https://master.php.net/note/delete/68753/integrated
Del: useless -- https://master.php.net/note/delete/68753/useless
Del: bad code -- https://master.php.net/note/delete/68753/bad+code
Del: spam -- https://master.php.net/note/delete/68753/spam
Del: non-english -- https://master.php.net/note/delete/68753/non-english
Del: in docs -- https://master.php.net/note/delete/68753/in+docs
Del: other reasons-- https://master.php.net/note/delete/68753
Reject -- https://master.php.net/note/reject/68753
Search -- https://master.php.net/manage/user-notes.php