简体   繁体   中英

PHP MYSQL Display results where day is todays date

$today = CURDATE();
$result = mysqli_query($con,"SELECT * FROM Persons WHERE Day ='"$today"'");

The columns are: Name
Day
Time
Reg

And a select * from Persons works fine.

You should be able to just specify your current date in the query (ie no need to calculate in PHP). This would also give you more consistant time handling in case web server and MySQL server have different timezones.

If Day is datetime or timestamp field use this:

SELECT * FROM Persons WHERE Day LIKE CONCAT(CURRENT_DATE(),'%')

If Day is date field use this:

SELECT * FROM Persons WHERE Day = CURRENT_DATE()

If day is a datetime, then use:

SELECT *
FROM Person
WHERE Day >= CURRENT_DATE and Day < CURRENT_DATE + interval 1 day;

The use of like for dates is bad practice, because it requires converting the date to a string. This prevents an index from being used.

If day has no time component, the above will work, but you can simplify it to:

SELECT *
FROM Person
WHERE Day = CURRENT_DATE;

You can also write:

SELECT *
FROM Person
WHERE date(Day) = CURRENT_DATE ;

(The parentheses on CURRENT_DATE are optional.)

$today = CURDATE();
$result = mysqli_query($con,"SELECT * FROM Persons WHERE Day LIKE '$today'");

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