note 83326 deleted from function.date by tularis
| From: | tularis@php.net | Date: | Thu, 09 Feb 2012 21:01:30 +0000 |
| Subject: | note 83326 deleted from function.date by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-186009@lists.php.net to get a copy of this message | ||
Note Submitter: wulf dot kaiser at mpimf-heidelberg dot mpg dot de
----
For output formatting of a SAMP based seminar announcement system, i had to fetch the date of every
friday of a given month in a given year. Here's what i did:
<?php
$givenYear = $_GET["givenYear"]; # assume "2006"
$givenMonth = $_GET["givenMonth"]; # assume "12"
if ($givenMonth != '12') {
$nextGivenMonth = "1";
$nextGivenYear = $givenYear + 1;}
else {
$nextGivenMonth = $givenMonth + 1;
$nextGivenYear = $givenYear;}
# Get the first weekday of the month
$firstDayOfMonth = date("d", mktime(0, 0, 0, $givenMonth, 1, $givenYear));
$firstWeekDayOfMonth = date("l", mktime(0, 0, 0, $givenMonth, 1, $givenYear));
# Count days to first Friday
switch ($firstWeekDayOfMonth) {
case 'Monday': $numOfDaysToFirstFriday = "4"; break;
case 'Tuesday': $numOfDaysToFirstFriday = "3"; break;
case 'Wednesday': $numOfDaysToFirstFriday = "2"; break;
case 'Thursday': $numOfDaysToFirstFriday = "1"; break;
case 'Friday': $numOfDaysToFirstFriday = "0"; break;
case 'Saturday': $numOfDaysToFirstFriday = "6"; break;
case 'Sunday': $numOfDaysToFirstFriday = "5"; break;}
# Get first Friday's date
$numOfDaysToFirstFriday = 1 + $numOfDaysToFirstFriday;
$firstFridayOfMonthDate = date("d.m.Y", mktime(0, 0, 0, $givenMonth,
$numOfDaysToFirstFriday, $givenYear));
$firstFridayOfMonthDay = date("d", mktime(0, 0, 0, $givenMonth,
$numOfDaysToFirstFriday, $givenYear));
# Get the last weekday of the month
$lastDayOfMonth = date("d", strtotime("-1 day",
strtotime(date("$nextGivenYear-$nextGivenMonth-01"))));
$lastWeekDayOfMonth = date("l", strtotime("-1 day",
strtotime(date("$nextGivenYear-$nextGivenMonth-01"))));
# Count days to last Friday
switch ($lastWeekDayOfMonth) {
case 'Monday': $numOfDaysToLastFriday = "3"; break;
case 'Tuesday': $numOfDaysToLastFriday = "4"; break;
case 'Wednesday': $numOfDaysToLastFriday = "5"; break;
case 'Thursday': $numOfDaysToLastFriday = "6"; break;
case 'Friday': $numOfDaysToLastFriday = "0"; break;
case 'Saturday': $numOfDaysToLastFriday = "1"; break;
case 'Sunday': $numOfDaysToLastFriday = "2"; break;}
# Get last Friday's date
$numOfDaysToLastFriday = $lastDayOfMonth - $numOfDaysToLastFriday;
$lastFridayOfMonthDate = date("d.m.Y", mktime(0, 0, 0, $givenMonth,
$numOfDaysToLastFriday, $givenYear));
$lastFridayOfMonthDay = date("d", mktime(0, 0, 0, $givenMonth, $numOfDaysToLastFriday,
$givenYear));
$divisor = $lastFridayOfMonthDay - $firstFridayOfMonthDay;
$divisor = $divisor / 7;
global $divisor;
# Get the dates of all Fridays in the given Month (can be either 4 or 5)
if ($divisor=='3') {
$firstFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$numOfDaysToFirstFriday, $givenYear));
$secondFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$lastFridayOfMonthDay - 14, $givenYear));
$thirdFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$lastFridayOfMonthDay - 7, $givenYear));
$lastFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$numOfDaysToLastFriday, $givenYear));}
else if ($divisor=='4') {
$firstFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$numOfDaysToFirstFriday, $givenYear));
$secondFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$lastFridayOfMonthDay - 21, $givenYear));
$thirdFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$lastFridayOfMonthDay - 14, $givenYear));
$fourthFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$lastFridayOfMonthDay - 7, $givenYear));
$lastFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth,
$numOfDaysToLastFriday, $givenYear));}
?>
Comments, suggestions and bugfixes are welcome ;-))