正文:
使用 WordPress 获取今天、昨天、本周、本月注册用户统计时遇到一个问题,即日期是以协调世界时(UTC)表示的。那么如何将其转换为东八区时间(UTC+8)后再进行统计呢?
可以使用 CONVERT_TZ(`user_registered`,’+00:00′,’+08:00′) 函数来实现转换。
代码如下:
add_action('wp_dashboard_setup', 'erphp_user_total_dashboard_widgets' );
function erphp_user_total_dashboard_widgets() {
if(current_user_can('administrator')){
add_meta_box( 'erphp_user_total_widget', '注册统计', 'erphp_user_total_widget_function','dashboard', 'normal', 'core' );
}
}
function erphp_user_total_widget_function(){
global $wpdb;
$today_user = $wpdb->get_row("SELECT count(ID) as ct FROM $wpdb->users WHERE TO_DAYS(NOW())- TO_DAYS(CONVERT_TZ(`user_registered`,'+00:00','+08:00')) = 0");
$yestoday_user = $wpdb->get_row("SELECT count(ID) as ct FROM $wpdb->users WHERE TO_DAYS(NOW())- TO_DAYS(CONVERT_TZ(`user_registered`,'+00:00','+08:00')) = 1");
$week_user = $wpdb->get_row("SELECT count(ID) as ct FROM $wpdb->users WHERE YEARWEEK(date_format(CONVERT_TZ(`user_registered`,'+00:00','+08:00'),'%Y-%m-%d')) = YEARWEEK(now())");
$month_user = $wpdb->get_row("SELECT count(ID) as ct FROM $wpdb->users WHERE DATE_FORMAT( CONVERT_TZ(`user_registered`,'+00:00','+08:00'), '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )");
echo '<div class="activity-block"><ul style="margin:0 -10px;overflow:hidden">';
echo '<li style="margin:0 10px 10px;float:left;width:calc(50% - 20px);border:1px solid #f0f0f1;padding:10px;box-sizing:border-box">
<div style="font-size:19px;margin-bottom:20px;">今日注册</div>
<div><a style="font-size:16px" href="'.admin_url('users.php').'">'.($today_user?$today_user->ct:'0').' 用户</a></div>
</li>';
echo '<li style="margin:0 10px 10px;float:left;width:calc(50% - 20px);border:1px solid #f0f0f1;padding:10px;box-sizing:border-box">
<div style="font-size:19px;margin-bottom:20px;">昨日注册</div>
<div><a style="font-size:16px" href="'.admin_url('users.php').'">'.($yestoday_user?$yestoday_user->ct:'0').' 用户</a></div>
</li>';
echo '<li style="margin:0 10px 10px;float:left;width:calc(50% - 20px);border:1px solid #f0f0f1;padding:10px;box-sizing:border-box">
<div style="font-size:19px;margin-bottom:20px;">本周注册</div>
<div><a style="font-size:16px" href="'.admin_url('users.php').'">'.($week_user?$week_user->ct:'0').' 用户</a></div>
</li>';
echo '<li style="margin:0 10px 10px;float:left;width:calc(50% - 20px);border:1px solid #f0f0f1;padding:10px;box-sizing:border-box">
<div style="font-size:19px;margin-bottom:20px;">本月注册</div>
<div><a style="font-size:16px" href="'.admin_url('users.php').'">'.($month_user?$month_user->ct:'0').' 用户</a></div>
</li>';
echo '</ul></div>';
}
转载请注明:汇站网 » 获取 WordPress 网站的今天、昨天、本周和本月的注册用户统计