note 93007 added to function.date
| From: | christophermmann at gmail dot com | Date: | Wed, 19 Aug 2009 11:44:43 +0000 |
| Subject: | note 93007 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159632@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:
<?
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
?>
----
Server IP: 208.69.120.35
Probable Submitter: 80.47.132.96
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/93007
Del: integrated -- https://master.php.net/note/delete/93007/integrated
Del: useless -- https://master.php.net/note/delete/93007/useless
Del: bad code -- https://master.php.net/note/delete/93007/bad+code
Del: spam -- https://master.php.net/note/delete/93007/spam
Del: non-english -- https://master.php.net/note/delete/93007/non-english
Del: in docs -- https://master.php.net/note/delete/93007/in+docs
Del: other reasons-- https://master.php.net/note/delete/93007
Reject -- https://master.php.net/note/reject/93007
Search -- https://master.php.net/manage/user-notes.php