note 71650 deleted from function.getdate by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 14:59:53 +0000 |
| Subject: | note 71650 deleted from function.getdate by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197285@lists.php.net to get a copy of this message | ||
Note Submitter: Jared Armstrong
----
A nice little function I wrote to determine what number occurrence weekday it is of the month for a
given timestamp. (I.e. 2nd Friday, or the 3rd Thursday)
Eg: print_r(getWeekdayOccurrence(mktime(0, 0, 0, 12, 1, 2006)));
Outputs: Array ( [0] => 1 [1] => Friday ) [The first friday]
Eg. print_r(getWeekdayOccurrence(mktime(0, 0, 0, 8, 17, 2009)));
Outputs: Array ( [0] => 3 [1] => Monday ) [The third Monday]
<?php
function getWeekdayOccurrence($time) {
$month = intval(date("m", $time)); $day = intval(date("d", $time));
for ($i = 0; $i < 7; $i++) {
$days[] = date("l", mktime(0, 0, 0, $month, ($i+1), date("Y", $time)));
}
$posd = array_search(date("l", $time), $days);
$posdm = array_search($days[0], $days) - $posd; /
return array((($day+$posdm+6)/7), $days[$posd]);
}
?>