Reputation: 1
I am trying to select data from mySQL database which was inserted today, however this query is working but it is returning 0 results. There is one row in my database which should be returned. Any help is greatly appreciated! Thanks!
$sql = "SELECT * FROM main_table WHERE DATE (date)= CURDATE()";
Upvotes: 0
Views: 697
Reputation: 135
I recommend that, unless you need milisecond information, you always store date information in Unix Timestamp. It is lighter to store, since it is only a integer value, is faster to retrieve and is universal, since it is always based on UTC.
Specially in PHP, converting date information to (time()
and strtotime
) and from (date()
) a unix timestamp is pretty easy. This way no matter where your user is, you can always show correct information in local time with almost no effort.
In your case you can calculate de timestamp for today 0 AM an today 12PM and then look for it in MySQL as:
$sql = "SELECT * FROM main_table WHERE `date`>{$dayStart}" AND `date`<{$dayEnd};
Upvotes: 3
Reputation: 17386
Either u can use direct mysql date function or php date functions..Now I am using php function
$today = date('Y-m-d'); //To get the Current date as 2014-10-12
$sql = "SELECT * FROM main_table WHERE `date` = '$today'";
Since your column name is date (Same as that of DATE function in sql) use date
..``
Upvotes: 0
Reputation: 108390
That would really depend on the actual datatype of the date
column in main_table
, how that column is defined, and whats stored in that column.
If that column is MySQL DATE
datatype, then
WHERE date = DATE(NOW())
If that column is DATETIME
or TIMESTAMP
datatype, then
WHERE date >= DATE(NOW()) AND date < DATE(NOW()) + INTERVAL 1 DAY
If that column is CHAR or VARCHAR, then it depends on the actual values stored. If that's stored in format 'mm/dd/yyyy' -- i.e. two digit month and two digit day (with leading zero) and four digit year, separated by slashes -- then
WHERE date = DATE_FORMAT(NOW(),'%m/%d/%Y')
Upvotes: 2