You are here

function birthdays_get_birthdays_by_days in Birthdays 5

Same name and namespace in other branches
  1. 6 birthdays.module \birthdays_get_birthdays_by_days()

Get all birthdays of the upcomming X days

Integer stating the amount of days to look forward, including today.

Return value

array An array containing user objects meeting the criteria

3 calls to birthdays_get_birthdays_by_days()
birthdays_block in ./birthdays.module
Implementation of hook_block
birthdays_get_todays_birthdays in ./birthdays.module
Helper function for displaying only todays birthdays
_birthdays_send_admin_message in ./birthdays.module
Sends e-mail to administrator once a day as reminder about the upcoming 7 days

File

./birthdays.module, line 809
The Birthdays module allows users to add their birthday to their profile. It lists birthdays on a seperate page and in different blocks. Users can receive an e-mail on their birthday automatically, and the administrator can receive daily reminders of…

Code

function birthdays_get_birthdays_by_days($amount) {
  $birthdays = array();

  // Current user, needed for timezone information
  global $user;

  // $amount should be larger or equal to 1
  if ($amount < 1) {
    $amount = 1;
  }

  // Get user time zone
  // needed to determine what day 'today' is in the timezone of the user/website.
  if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
    $timezone = $user->timezone;
  }
  else {

    // else use timezone of Drupal installation
    $timezone = variable_get('date_default_timezone', 0);
  }

  // MySQL prior to 4.1.1 has no option to use UTC, while drupal uses UTC.
  // This is compensated by subtracting the machines timezone from the Drupal timezone.
  // I believe the assumption is that the HTTP-server has the same timezone as the MySQL server.
  $timezone -= date('Z');

  // Hack to look further than the end of the year, if needed.
  $current_year = date('Y');
  $next_year = $current_year + 1;

  /* Query:
     - All dates are compensated for the timezone. This makes sure that someone in Asia will see the birthdays
       of day 2 while someone in America still sees day 1.
     - Blocked users are not shown
     - Users that haven't logged in yet are also not shown (Drupal prohibits accessing their profile, thus showing
       a link to the profile is unwanted). This is a anti spammers method.
     - First part selects all birthdays that are in the next year when the interval exceeds the end of this year.
     - Second part selects all birthdays that are between the begin and end date and are in the current year
     */
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $result = db_query("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n        AND (\n        (\n          DATE_FORMAT({dob}.birthday,'{$next_year}%%m%%d') - DATE_FORMAT(ADDDATE(ADDDATE(NOW(),INTERVAL %d SECOND),INTERVAL %d DAY),'%%Y%%m%%d') < 0\n        )\n        OR\n        (\n          DATE_FORMAT({dob}.birthday,'{$current_year}%%m%%d') - DATE_FORMAT(ADDDATE(NOW(),INTERVAL %d SECOND),'%%Y%%m%%d') >= 0\n          AND\n          DATE_FORMAT({dob}.birthday,'{$current_year}%%m%%d') - DATE_FORMAT(ADDDATE(ADDDATE(NOW(),INTERVAL %d SECOND),INTERVAL %d DAY),'%%Y%%m%%d') < 0\n        ) )\n        ORDER BY MONTH({dob}.birthday), DAYOFMONTH({dob}.birthday), YEAR({dob}.birthday), {users}.name", $timezone, $amount, $timezone, $timezone, $amount);
      break;
    case 'pgsql':
      $result = db_query("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n        AND (\n        (\n          cast(to_char({dob}.birthday,'{$next_year}MMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds' + INTERVAL '%d days','YYYYMMDD') as integer) < 0\n        )\n        OR\n        (\n          cast(to_char({dob}.birthday,'{$current_year}MMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds','YYYYMMDD') as integer) >= 0\n          AND\n          cast(to_char({dob}.birthday,'{$current_year}MMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds' + INTERVAL '%d days','YYYYMMDD') as integer) < 0\n        ) )\n        ORDER BY date_part('month', {dob}.birthday), date_part('day', {dob}.birthday), date_part('year', {dob}.birthday), {users}.name", $timezone, $amount, $timezone, $timezone, $amount);
      break;
  }
  while ($account = db_fetch_object($result)) {
    $birthdays[] = $account->uid;
  }

  // Return array of uids that have their birthday
  return $birthdays;
}