Last active
October 6, 2021 12:55
-
-
Save stephenharris/5640698 to your computer and use it in GitHub Desktop.
Get all users registered between two dates
This file contains 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 all users registered after $start and before $end (dates in yyyy-mm-dd format) | |
* | |
* Based on my answer to this question: http://wordpress.stackexchange.com/questions/51485/how-can-i-query-all-users-who-registered-today/51492#51492 | |
* | |
* @param (string) $start - start date in yyyy-mm-dd format | |
* @param (string) $end - end date in yyyy-mm-dd format | |
*/ | |
function wpse51485_get_users_registered($start='', $end='' ){ | |
global $wpdb; | |
if( empty($start) ) | |
$date = date('Y-m-d'); | |
if ( empty($end) ) | |
$end = $start; | |
//Should probably validate input and throw up error. In any case, the following ensures the query is safe. | |
$start_dt = new DateTime($start. ' 00:00:00'); | |
$s = $start_dt->format('Y-m-d H:i:s'); | |
$end_dt = new DateTime($end.' 23:59:59'); | |
$e = $end_dt->format('Y-m-d H:i:s'); | |
$sql = $wpdb->prepare("SELECT wp_users.* FROM wp_users WHERE 1=1 AND CAST(user_registered AS DATE) BETWEEN %s AND %s ORDER BY user_login ASC",$s,$e); | |
$users = $wpdb->get_results($sql); | |
return $users; | |
} | |
?> |
Great! Thanks for sharing this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome - gave me a headstart today. Thanks!
A quick improvement would be to use
$wpdb->users
as your table names as thewp_
prefix can be changed in config!