简体   繁体   中英

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).

When i use sql query like

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

My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.

Note, i will not change INSERT date time!

Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.

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

You can also replace CURDATE() with NOW() , CURRENT_DATE() , and 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

plz change your statement equal operator to greater than

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

as by default if time portion is not present then it is putting 00:00...

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