获取30天前(或更长时间)注册的用户

时间:2012-09-19 作者:Jacek Jagiełło

如何(在wordpress multisite中)获取30天前或更多注册用户的id?我正在建立为期30天的免费试用系统。我想有一个功能,检查用户何时注册,以及已经过了多少天。函数应返回经过的天数。

很容易获得用户注册时间:

global $current_user;
get_currentuserinfo();
echo $current_user->user_registered;
这是echo:

2012-09-16 6:25:47
现在,如何检查距离上述时间过去了多少天?

3 个回复
SO网友:kaiser

这就是功能human_time_diff( $from, $to );, 它将比较两个UNIX时间戳。仅使用mktime() 在…上$GLOBALS[\'current_user\']->user_registered; 并将其与当前UNIX时间进行比较。

SO网友:Brooke.

如果你想更系统地使用脚本,你可以这样做

$timestamp_start = strtotime($startdate);
$timestamp_end = strtotime($enddate);
$difference = abs($timestamp_end - $timestamp_start);
然后你可以写一个脚本来检查是否超过30天

if ($difference >= 2592000){
//send invite code
}
通过这样做,您还可以获得一个完整的“仅几天”版本

$days = floor($difference/(60*60*24));
echo \'Days \'.$days;

Source

SO网友:Jacek Jagiełło

谢谢你的回答,我结合了你的解决方案,它很有效!

human\\u time\\u diff,不适合我的脚本,因为它的返回时间是分钟、小时和天,带有“ago”字样。因此,我创建了自己的函数,该函数只返回天,没有“ago”。

功能代码:

function count_days($from){

    $start_time = $from;
    $current_time = time();

    //convert given date to the unix time (in seconds)
    $unix_start_time = date("U",strtotime($start_time));

    //count the difference between current time and given date
    $diff = (int) abs($current_time - $unix_start_time);

    //Now, we change seconds for days
    if ($diff >= 86400) {
        $days = round($diff / 86400);
        if ($days <= 1) $days = 1;  
    }

    //returns days
    return $days;
}
函数的第一个参数是日期,例如22-09-2012。接下来,将该日期转换为unix秒,然后我们可以从该日期中减去当前时间。这样,我们可以计算自给定日期以来经过的天数。耶!

再次感谢!

结束

相关推荐

Ajax for non-logged-in users

我正在开发一个插件,它可以从未登录用户(如Like按钮)捕获一些数据,并根据post\\u id将其输入数据库。我的插件对登录用户很好,但非登录用户在AJAX中会产生“-1”响应。我猜这与写入数据库的身份验证障碍有关,但我正在寻找一种解决方法。有什么想法吗?编辑:以下是代码:http://pastebin.com/ghtvJNiW我是根据Love-It AJAX plugin 皮平。他在教程中说,这只适用于登录用户,但我需要让它也适用于未登录用户。