note 8685 modified in ref.datetime by irc-html
| From: | irc-html@php.net | Date: | Sun, 20 Jan 2002 02:07:32 +0000 |
| Subject: | note 8685 modified in ref.datetime by irc-html | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-25550@lists.php.net to get a copy of this message | ||
Need the week of the month?
Heres a function that should do the trick:
/*Call the function as:
weekofmonth(year,month,date)
- either can be int's or strings with leading 0's.
If theres no arguements today will be the arguements.
Output is in the form "YYYYmmw" where "w" represents the week of the month.
*/
function weekofmonth($yr=0,$mn=0,$dd=0) {
if(!($yr>0&$mn>0&$dd>0)) {
$yr=date("Y");$mn=date("m");$dd=date("d"); }
$thisweek = strftime("%U",mktime(0,0,0,$mn,$dd,$yr));
$firstweekofmonth = strftime("%U", mktime(0, 0, 0, date("m", mktime(0, 0, 0,
$mn, $dd+(6-date("w", mktime(0, 0, 0, $mn, $dd, $yr))), $yr)), 1,$ yr));
$thisweek = (abs($thisweek-$firstweekofmonth)>5) ? $firstweekofmonth:$thisweek;
return date("Y", mktime(0, 0, 0, $mn, $dd+(6-date("w", mktime(0, 0, 0, $mn,
$dd, $yr))), $yr)) . date("m", mktime(0, 0, 0, $mn, $dd+(6-date("w",mktime(0, 0,
0, $mn, $dd, $yr))), $yr)) . (($thisweek - $firstweekofmonth) + 1);
}
--was--
Need the week of the month?
Heres a function that should do the trick:
/*Call the function as:
weekofmonth(year,month,date)
- either can be int's or strings with leading 0's.
If theres no arguements today will be the arguements.
Output is in the form "YYYYmmw" where "w" represents the week of the month.
*/
function weekofmonth($yr=0,$mn=0,$dd=0) {
if(!($yr>0&$mn>0&$dd>0)) {
$yr=date("Y");$mn=date("m");$dd=date("d"); }
$thisweek = strftime("%U",mktime(0,0,0,$mn,$dd,$yr));
$firstweekofmonth =
strftime("%U",mktime(0,0,0,date("m",mktime(0,0,0,$mn,$dd+(6-date("w",mktime(0,0,0,$mn,$dd,$yr))),$yr)),1,$yr));
$thisweek = (abs($thisweek-$firstweekofmonth)>5) ? $firstweekofmonth:$thisweek;
return
date("Y",mktime(0,0,0,$mn,$dd+(6-date("w",mktime(0,0,0,$mn,$dd,$yr))),$yr)) .
date("m",mktime(0,0,0,$mn,$dd+(6-date("w",mktime(0,0,0,$mn,$dd,$yr))),$yr)) .
(($thisweek - $firstweekofmonth) + 1);
}
http://www.php.net/manual/en/ref.datetime.php