简体   繁体   中英

Count Number of users registered today and yesterday PHP

I want to echo number of users registered on my site's admin panel today[counting] and yesterday using PHP.

I am saving their signup date in this format

2017-01-09 20:32:40

After googling i found this

$daterange = "24 HOUR"; 
$sql = "SELECT COUNT(*)  
FROM ad_total_today      
WHERE DATE_SUB(CURDATE(),INTERVAL $daterange) 
<= FROM_UNIXTIME(date_time)"; 

$res = mysql_query($sql); 
$today = mysql_result ($res, 0, 0);  

But I cant seem to understand it.Can anyone help me echo Total number of users registered today and yesterday?

A solution is to use the MySQL function CURDATE() , format your column to date-only with DATE() (so we don't have to deal with hour/seconds/minutes). Then you select what's equal to today or yesterday. I've used the SUBCAT() function to subtract one day from CURDATE() .

SELECT COUNT(*) as `count`
FROM ad_total_today 
WHERE DATE(date_time) = CURDATE() 
   OR DATE(date_time) = SUBDATE(CURDATE(), 1)

Alternatively, you can use IN instead of two conditions.

SELECT COUNT(*) as `count`
FROM ad_total_today 
WHERE DATE(date_time) IN (CURDATE(), SUBDATE(CURDATE(), 1))

You should really stop using MySQL functions, they are old, deprecated, insecure and no longer maintained. Switch to either PDO or MySQLi - and don't forget to use parameterized queries with placeholders when dealing with variables in your query - this is the only way to protect your data from SQL injection.

References and readingmaterial

Try Below Query

SELECT COUNT(*)  
FROM ad_total_today      
WHERE DATE_SUB IN (CURDATE(), CURDATE() + INTERVAL 1 DAY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM