note 93007 modified in function.date by danbrown
| From: | danbrown@php.net | Date: | Wed, 19 Aug 2009 15:36:42 +0000 |
| Subject: | note 93007 modified in function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159639@lists.php.net to get a copy of this message | ||
I couldn't find a function to do this properly. All functions I could find would use the
current month as an argument.
Anyway, this function will find the occurence of a particular weekday in a particular month. For
instance, the second wednesday in june 2009 would be called by:
<?php
echo findFirstDayofWeek(6,2009, "Wednesday", 2);
?>
Where 6 is "June", 2009 is the year, Wednesday is the weekday we want and 2 is the 2nd
occurence.
<?php
function findFirstDayOfWeek($month, $year, $day, $offset){ // supply the month, year, day and offset
$FirstDay = mktime(0, 0, 0, $month, 1, $year); // Get the first day of the month in question
$DayName = date("l", $FirstDay); //set the name of the first day for the while loop
$CurrentStamp = $FirstDay; // set a disposable variable for use in the while loop
$Results = 0; //set the number of results (for use with the offset argument)
While($Results != $offset){ //while the number of results does not equal the offset we are looking
for
$CurrentStamp = $CurrentStamp + 86400; // add a day
if(date("l", $CurrentStamp) == $day){ // if the name of the weekday we are currently
looping at is the same name as the argument supplied, set the date variable to that weekday and
increment the results variable by 1
$Date = date("Y-m-d", $CurrentStamp);
$Results++;
}
}
if(date("n", $CurrentStamp) != $month){ // this line checks whether or not the date that
the while loop has found is in the same month we are asking form otherwise, there must be no
"3rd friday in august"
return "No weekday at this offset";
} else {
return $Date; //send back the date
}
}
echo findFirstDayofWeek(9,2009, "Friday", 5); // returns a formatted date for the first
instance of a certain day in the argumental month
?>
--was--
I couldn't find a function to do this properly. All functions I could find would use the
current month as an argument.
Anyway, this function will find the occurence of a particular weekday in a particular month. For
instance, the second wednesday in june 2009 would be called by:
<?
echo findFirstDayofWeek(6,2009, "Wednesday", 2);
?>
Where 6 is "June", 2009 is the year, Wednesday is the weekday we want and 2 is the 2nd
occurence.
<?
function findFirstDayOfWeek($month, $year, $day, $offset){ // supply the month, year, day and offset
$FirstDay = mktime(0, 0, 0, $month, 1, $year); // Get the first day of the month in question
$DayName = date("l", $FirstDay); //set the name of the first day for the while loop
$CurrentStamp = $FirstDay; // set a disposable variable for use in the while loop
$Results = 0; //set the number of results (for use with the offset argument)
While($Results != $offset){ //while the number of results does not equal the offset we are looking
for
$CurrentStamp = $CurrentStamp + 86400; // add a day
if(date("l", $CurrentStamp) == $day){ // if the name of the weekday we are currently
looping at is the same name as the argument supplied, set the date variable to that weekday and
increment the results variable by 1
$Date = date("Y-m-d", $CurrentStamp);
$Results++;
}
}
if(date("n", $CurrentStamp) != $month){ // this line checks whether or not the date that
the while loop has found is in the same month we are asking form otherwise, there must be no
"3rd friday in august"
return "No weekday at this offset";
} else {
return $Date; //send back the date
}
}
echo findFirstDayofWeek(9,2009, "Friday", 5); // returns a formatted date for the first
instance of a certain day in the argumental month
?>
http://php.net/manual/en/function.date.php