简体   繁体   English

数据库日期=今天的日期

[英]database date = todays date

I am trying to select data from mySQL database which was inserted today, however this query is working but it is returning 0 results. 我正在尝试从今天插入的mySQL数据库中选择数据,但是此查询有效,但返回0个结果。 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()"; $ sql =“ SELECT * FROM main_table WHERE DATE(date)= CURDATE()”;

I recommend that, unless you need milisecond information, you always store date information in Unix Timestamp . 我建议,除非需要毫秒信息,否则始终将日期信息存储在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. 由于它只是一个整数值,因此存储起来比较轻便,因为它始终基于UTC,因此检索起来更快并且通用。

Specially in PHP, converting date information to ( time() and strtotime ) and from ( date() ) a unix timestamp is pretty easy. 特别是在PHP中,将日期信息转换为( time()strtotime )以及从( date() )转换为unix时间戳非常容易。 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: 在您的情况下,您可以计算今天0 AM到今天12PM的默认时间戳,然后在MySQL中查找为:

$sql = "SELECT * FROM main_table WHERE `date`>{$dayStart}" AND `date`<{$dayEnd};

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. 这实际上取决于main_table date列的实际数据类型,该列的定义方式以及该列中存储的内容。

If that column is MySQL DATE datatype, then 如果该列是MySQL DATE数据类型,则

    WHERE date = DATE(NOW())

If that column is DATETIME or TIMESTAMP datatype, then 如果该列是DATETIMETIMESTAMP数据类型,则

    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. 如果该列是CHAR或VARCHAR,则取决于存储的实际值。 If that's stored in format 'mm/dd/yyyy' -- ie two digit month and two digit day (with leading zero) and four digit year, separated by slashes -- then 如果以“ mm / dd / yyyy”格式存储-即两位数的月份和两位数的日期(前导零)和四位数的年份(以斜杠分隔)-那么

    WHERE date = DATE_FORMAT(NOW(),'%m/%d/%Y')

Either u can use direct mysql date function or php date functions..Now I am using php function 您可以使用直接的mysql日期函数或php日期函数。现在我正在使用php函数

$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 ..`` 由于您的列名是date(与sql中的DATE函数相同),请使用date ..``

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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