Created
January 22, 2013 17:38
-
-
Save anonymous/4596551 to your computer and use it in GitHub Desktop.
Updated version of get upcoming birthdays, within an interval, such as month or week (default), based on gist by @rythie. Two types of queries (pick which you want), one uses separate meta data for DOB (day and month used), the other uses the DOB as a timestamp. Optional parameter for order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* get_birthdays | |
* @desc Get upcoming birthdays, within a month or week (default) | |
* @see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html | |
* @global object $wpdb | |
* @param string|int $type | |
* @param string $orderby (asc or desc) | |
* @return array | |
*/ | |
public static function get_birthdays($type = 'week', $orderby = 'ASC') { | |
global $wpdb; | |
switch($type) { | |
case 'month': | |
$interval = 30; | |
break; | |
case 'week': | |
default: | |
$interval = 7; | |
break; | |
} | |
if(is_int($type)) { | |
$interval = $type; | |
} | |
$order_by = in_array(strtoupper($orderby), array('ASC', 'DESC')) ? $orderby : 'ASC'; | |
// uses separate user meta data; day and month | |
$sql = 'SELECT user.* | |
FROM %1$s AS user | |
LEFT JOIN wp_usermeta as meta_month ON user.ID = meta_month.user_id AND meta_month.meta_key = "date_dob_month" | |
LEFT JOIN wp_usermeta as meta_day ON user.ID = meta_day.user_id AND meta_day.meta_key = "date_dob_day" | |
WHERE user.user_status = 0 | |
AND DATEDIFF( | |
DATE( | |
CONCAT( | |
YEAR(DATE_ADD(NOW(), INTERVAL %2$d DAY)), "-", | |
meta_month.meta_value, "-" , | |
meta_day.meta_value | |
) | |
), | |
DATE(NOW()) | |
) BETWEEN 0 AND %2$d | |
ORDER BY UNIX_TIMESTAMP( | |
CONCAT( | |
YEAR(DATE_ADD(NOW(), INTERVAL %2$d DAY)), "-", | |
meta_month.meta_value, "-" , | |
meta_day.meta_value | |
) | |
) %3$s'; | |
// based on the user dob stored as unix timestamp | |
// because of potential negative epoch, use the follow http://www.epochconverter.com/programming/mysql-from-unixtime.php#negavtiveEpoch | |
$sql = 'SELECT user.* | |
FROM %1$s AS user | |
LEFT JOIN wp_usermeta as meta ON user.ID = meta.user_id AND meta.meta_key = "date_dob" | |
WHERE user.user_status = 0 | |
AND meta.meta_value != "" | |
AND DATEDIFF( | |
DATE( | |
CONCAT( | |
YEAR(DATE_ADD(NOW(), INTERVAL %2$d DAY)), "-", | |
MONTH(DATE(DATE_ADD(FROM_UNIXTIME(0), INTERVAL meta.meta_value SECOND))), "-" , | |
DAY(DATE(DATE_ADD(FROM_UNIXTIME(0), INTERVAL meta.meta_value SECOND))) | |
) | |
), | |
DATE(NOW()) | |
) BETWEEN 0 AND %2$d | |
ORDER BY UNIX_TIMESTAMP(DATE_ADD(FROM_UNIXTIME(0), INTERVAL meta.meta_value SECOND)) %3$s'; | |
$query = sprintf($sql, $wpdb->users, $interval, $order_by); | |
return $wpdb->get_results($query, OBJECT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment