Player DOB uses

A client uses a player's age to restrict program registration. The restriction is based on the player's age on the start date of the division.

In addition, on the front end, they display a list of players' birthdays that occur in the current week.

1) Age on date helper:

Below is the util::getAgeOnDate() helper to calculate an age based on a birthday and a given date. This is useful in determining a player's qualifying age on the start date of a program ($year_diff)  as opposed to their current age.


/**
 * Calculate age based on birthdate and a given date.
 * based on http://www.populate.it/age_on_date_php.html
 *
 * @param dob: format 'MM/DD/YYYY'
 * @param date: format 'MM/DD/YYYY'
 * @return array
 */
function getAgeOnDate($dob, $date) {

$birthdate = explode('/', $dob);
$givendate = explode('/', $date);

$year_diff    = $givendate[2] - $birthdate[2];
$month_diff = $givendate[0] - $birthdate[0];
$day_diff     = $givendate[1] - $birthdate[1];

if ($day_diff < 0) {
$month_diff--;
$day_diff = cal_days_in_month (CAL_GREGORIAN, $birthdate[0], $birthdate[2]) - (-1 * $day_diff);
}

if ($month_diff < 0){
$year_diff--;
$month_diff = 12 - (-1 * $month_diff);
}
return array($year_diff, $month_diff, $day_diff);
}

2) This Week's Birthdays:

Below is the method used to get a list of players whose age on the first day of the week does not match their age on the last day of the week, thereby indicating that their birthday falls within those 2 days.
/**
 * Return list of players birthdays this week.
 *
 * @param filters array of filters/sort flags applied to list
 * @return array
 */
public function getThisWeekBirthdays($filters = array()) {
// Init list
$list     = array();
$today    = date('l');
$interval = '7';

// Get the start date of the week
if ($today == 'Monday') {
$start_week = date('Y-m-d', strtotime("last Sunday midnight"));
} else {
$start_week = date('Y-m-d', strtotime("last Sunday"));
}

// Get list
try {
$sql =
"SELECT
p.id,
p.fname,
p.lname,
p.dob
FROM
profiles p
JOIN team_players tp ON tp.profile_id = p.id
WHERE
tp.roster_id = '" . mysql_real_escape_string($filters['id']) . "'
AND FLOOR(DATEDIFF(DATE('" . $start_week . "'), dob) / 365.25) <>
FLOOR(DATEDIFF(DATE_ADD(DATE('" . $start_week . "'), INTERVAL " . $interval . " DAY), dob) / 365.25)
ORDER BY
p.lname ASC
";

$res = $this->db->query($sql)->result(FALSE);
foreach ($res as $row) {
$list[$row['id']] = $row;
}
} catch (Kohana_Database_Exception $e) {
Kohana::log('error', "{$e->getFile()}: {$e->getLine()}\n{$e->getMessage()}");
}

// Return
return $list;
}