简体   繁体   English

PHP计数更改日期格式的mysql行

[英]PHP count mysql row with changed date format

I have next problem: My table date format was: LIKE 2017-01-08 18:50:25 (with time). 我有下一个问题:我的表格日期格式为:LIKE 2017-01-08 18:50:25 (带时间)。

When i use sql query like 当我使用sql查询时

'SELECT date FROM table WHERE date = "2017-01-08"'

My row was empty, i need COUNT all row with same (today) date WITHOUT TIME. 我的行是空的,我需要COUNT个具有相同(今天)日期且没有时间的行。

Note, i will not change INSERT date time! 注意,我不会更改INSERT日期时间!

Use DATE() to get the date portion of the datetime field and compare it to today. 使用DATE()获取datetime字段的日期部分,并将其与今天进行比较。 Use COUNT() to get the number of records that match your query. 使用COUNT()获取与您的查询匹配的记录数。

SELECT count(*) FROM table WHERE DATE(date) = CURDATE()

You can also replace CURDATE() with NOW() , CURRENT_DATE() , and CURRENT_DATE 您还可以将CURDATE()替换为NOW()CURRENT_DATE()CURRENT_DATE

You can also use it in the following way 您也可以按以下方式使用它

'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'

the date_format is mysql function which return date according to your pattern the above pattern only return the Ymd from the datetime I hope it will help you date_format是mysql函数,它根据您的模式返回日期,上述模式仅从datetime返回Ymd,希望对您有所帮助

plz change your statement equal operator to greater than 请更改等于操作符的语句等于

'SELECT date FROM table WHERE date > "2017-01-08"' '从表WHERE日期中选择日期>“ 2017-01-08”“

as by default if time portion is not present then it is putting 00:00... 默认情况下,如果不存在时间部分,那么它将输入00:00 ...

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

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