我的MySQL数据库中有一个名为users的表。此表中有一个名为user_reg_date(bigint(12))的库,用于存储该用户的注册日期。
此列以UNIX时间戳格式存储注册日期。对于PHP中的UNIX时间戳和日期操作,我是个新手。现在我的要求是将当前日期传递给一个函数,并获取注册日期为当前日期的用户计数(即今天注册的用户计数)。
我不知道如何将当前日期传递给该函数,并获取将其注册日期作为当前日期的用户计数。下面我给出了我的PHP函数代码。
/*The argument `$todays_date` I'm passing is dummy, please guide me how to pass the date also*/
function GetRegisteredUsersCount($todays_date) {
$sql = " SELECT count(*) as registered_users_count FROM ".TBL_USERS;
$sql .= " WHERE user_reg_date ='".$todays_date."' ";
$this->mDb->Query( $sql);
$data = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE);
return $data;
}发布于 2013-10-21 16:03:13
由于您的日期存储为整数,因此您需要使用从今天开始到(不包括)第二天开始之间的范围进行查询。
$today = strtotime('today');
$tomorrow = strtotime('+1 day', $today);
$sql .= sprintf(
" WHERE user_reg_date >= %d AND user_reg_date < %d",
$today,
$tomorrow
);另请参阅:strtotime()
顺便说一句,建议在使用64位整数的平台上运行它。
发布于 2013-10-21 16:09:13
您应该将user_reg_date(bigint(12))字段类型更改为timestamp,并将其默认设置为CURRENT_TIMESTAMP。每当新用户注册时,它都会自动选择当前日期和时间。当你想知道今天有多少用户注册时,只需点击一个查询:-
$sql = "SELECT count(*) as registered_users_count from tb_users where datediff(CURDATE(),DATE(user_reg_date)) =0"https://stackoverflow.com/questions/19488751
复制相似问题