note 36564 deleted from function.getdate by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 15 Sep 2004 16:39:25 +0000 |
| Subject: | note 36564 deleted from function.getdate by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76762@lists.php.net to get a copy of this message | ||
Note Submitter: baogle at georgefox dot edu
----
in response to selino at selino dot com, and just because
i was making a calendar and wanted to calculate holidays,
i wrote this function, you can find the full commented source
@ http://cs.georgefox.edu/~bogle/code/dom.txt
/**
* function dayOfMonth
*
* finds either a specific occurence of a day based on name
* (i.e., monday, tuesday, etc.), after a certain day, or
* the last occurence of a day of the month.
*
* if no $occurence of is given, then it will look for the
* first occurence of that day. to find the last occurence
* of a day, then give a zero (0).
*
* if no after date is given then it will start on the first
* day of the month
*
* if any invalid data is passed then the function will return
* false
*
*/
function dayOfMonth($fmonth,
$fyear,
$day_to_find,
$occurenceOf = 1,
$after = 0)
{
$dtr = false;
$done = false;
if(checkdate($fmonth,1,$fyear) &&
$occurenceOf > -1 &&
$occurenceOf < 6 && $after < 32)
{
$cd = $after + 1;
if($occurenceOf != 0)
{
while(!$done)
{
if(date("w",mktime(0,0,0,$fmonth,$cd,$fyear))
== $day_to_find)
{
$dtr = $cd + (($occurenceOf - 1) * 7);
$done = true;
}
elseif($cd == 32)
{
$done = true;
}
$cd++;
}
}
else
{
$t = getdate(mktime(0,0,0,$fmonth+1,0,$fyear));
$daysinmonth = $t["mday"];
for($cd=$daysinmonth;$cd>0 && !$done;$cd--)
{
if(date("w",mktime(0,0,0,$fmonth,$cd,$fyear))
== $day_to_find)
{
$dtr = $cd;
$done = true;
}
}
}
}
return $dtr;
}
so if you wanted to know the fourth thursday of november,
which is thanksgiving
dayOfMonth(date("n"),date("Y"),4,4);
or if you wanted to know grandparents day, which is the
sunday after labor day, (labor day is the first monday in
september):
dayOfMonth(date("n"),date("Y"),0,1,
dayOfMonth(date("n"),date("Y"),1));
hope this helps
brian