note 77188 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 09 Dec 2007 13:40:58 +0000 |
| Subject: | note 77188 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133010@lists.php.net to get a copy of this message | ||
Note Submitter: wes.wysoft.com
----
Well I looked all over the place for this and couldn't find it so I made my own. Hopefully it
saves someone else some time. It will give you the occurances of a day in a month. For example if
you want to get the date of the 2 Tuesday in a month.
<?php
function weekDayOf($year,$month,$dayoftheweek=null,$occurance=null) {
/*
* returns the # occurance of the day of the week. Such as the 3rd sunday
* if occurance is null it will return the whole array. for the week days in that month
* it can be accessed by $date[$dayoftheweek][$occurance]=date;
*/
$mdays=date("t",mktime(0,0,0,$month,1,$year)); //days in the current month
$first_day = date('w',mktime(0,0,0,$month,1,$year)); // day of the week the month started
with
$day_oc = array(); // occurances of each day of the week
for($i=0;$i<$mdays;++$i){
$dayofweek = ($i+$first_day)%7;
if(!is_array($day_oc[$dayofweek])) {
$day_oc[$dayofweek] = array();
}
$day_oc[$dayofweek][]=$i+1;
}
return $dayoftheweek ? $day_oc[$dayofweek][$occurance] : $day_oc;
}
?>