| px | top | add code | search | signup | login | help |
<?php
function rfu_week_number ($date) {
/*
$date parameter must be passed in ISO format YYYY-DD-MM
e.g. 1 September 2001 is passed as 2001-09-01
Definition of RFU week number from http://www.rugbyinsussex.org.uk/faqs.htm
This is a long-standing system used by the RFU and by all Club Fixtures Secretaries to
identify exactly which date a match is to be made. Every Saturday has a 'RFU Week No',
starting with Number 1 as the first Saturday in September, (Saturdays in August are
denoted a 'A', 'B','C' etc), running through to the last Saturday in May as No. 26.
Where there is a fifth Saturday in a month, it is called an 'X' week and numbered from
1 (September). Thus, in Season 2002/2003, the fifth Saturday in November is called
'Week X3' and the fifth Saturday in March is called 'Week X7
*/
# Return null if Saturday in June or July
if (substr ($date, 5, 2) == "06" or substr ($date, 5, 2) == "07") return "";
# Return A, B, C, D or E if Saturday is in August
if (substr ($date, 5, 2) == "08") {
# Find first Saturday in August
$sat = strtotime (substr ($date, 0, 4)."-08-01");
while (date ("w", $sat) != "6") {
$sat = dateadd ("d", 1, $sat);
}
# Use ISO week number to calculate week letter
return chr (65 + get_week_number (strtotime ($date)) - get_week_number ($sat));
}
# Find first Saturday in September
if (date ("m", strtotime ($date)) < "06") {
$sat = strtotime ((substr ($date, 0, 4) - 1)."-09-01");
} else {
$sat = strtotime (substr ($date, 0, 4)."-09-01");
}
while (date ("w", $sat) != "6") {
$sat = dateadd ("d", 1, $sat);
}
# Go through each week until a match is found
$week = 1;
$month_week = 1;
$month = "09";
$month_number = 1;
while (strtotime ($date) > $sat) {
$sat = dateadd ("d", 7, $sat);
if ($month_week <= 4) {
$week = $week + 1;
$month_week = $month_week + 1;
}
if (date ("m", $sat) != $month) {
$month_week = 1;
$month = date ("m", $sat);
$month_number = $month_number + 1;
}
}
if ($month_week <= 4) {
return $week;
} else {
return "X".$month_number;
}
}
/*
Following three functions used to calculate week number. Taken from
http://px.sklar.com/code-pretty.html?code_id=28
Can be replaced in PHP 4.1 by date ("W", ...).
*/
/* weeknumber.php3
(int) get_week_number($timestamp)
Week number of year with Monday as first day of the week
(1...53). If the week containing January 1 has four or more days
in the new year, then it is considered week 1; otherwise, it is
week 53 of the previous year, and the next week is week 1. (See the
ISO 8601: 1988 standard.)
Adapted from GNU sh-utils for PHP3 by Stefan Röhrich, sr@linux.de,
http://home.pages.de/~sr/.
*/
function is_leap_year($year) {
if ((($year % 4) == 0 and ($year % 100)!=0) or ($year % 400)==0) {
return 1;
} else {
return 0;
}
}
/*
#define ISO_WEEK_START_WDAY 1 // Monday
#define ISO_WEEK1_WDAY 4 // Thursday
#define YDAY_MINIMUM (-366)
int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7;
return (yday
- (yday - wday + ISO_WEEK1_WDAY + big_enough_multiple_of_7) % 7
+ ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY);
*/
function iso_week_days($yday, $wday) {
return $yday - (($yday - $wday + 382) % 7) + 3;
}
function get_week_number($timestamp) {
$d = getdate($timestamp);
$days = iso_week_days($d["yday"], $d["wday"]);
if ($days < 0) {
$d["yday"] += 365 + is_leap_year(--$d["year"]);
$days = iso_week_days($d["yday"], $d["wday"]);
} else {
$d["yday"] -= 365 + is_leap_year($d["year"]);
$d2 = iso_week_days($d["yday"], $d["wday"]);
if (0 <= $d2) {
/* $d["year"]++; */
$days = $d2;
}
}
return (int)($days / 7) + 1;
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.