用 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主题安装、深度汉化、加速优化等各类网站建设服务,详询在线客服!