Jump to content

Simple PHP Date Comparison


arunkar

Recommended Posts

Hi,

 

I'm very new to PHP. I'm trying count the number of registrations today and display in the web page. Below is the code I'm using but I'm unable to compare:

 

The date is the mysql database is stored as 2008-03-27 21:09:41

 

$today = date("Y-m-d");

$resultCount = mysql_query("SELECT count(*) FROM users WHERE registerDate='$today' ");

 

its returning 0 but there are 2 registrations. So there is a logical error. I'm using the date functions here: http://sg2.php.net/date for reference.

 

Any ideas? ???

 

Thanks in advance. 

Link to comment
https://forums.phpfreaks.com/topic/98272-simple-php-date-comparison/
Share on other sites

Your field in the database is a DATETIME type. If you want to compare just the date, then your query must reference just the date (the mysql DATE() function will work.) Also, it is not necessary to use any slow php code to form today's date, just use the mysql CURDATE() function -

 

$resultCount = mysql_query("SELECT count(*) FROM users WHERE
DATE(registerDate) = CURDATE()");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.