简体   繁体   中英

How to compare date when its save as type text in database

I am using WPDB and here is my SQL

$date = date('d-m-Y');
$reservations = $wpdb->get_results( "SELECT * FROM reservation_db WHERE `date` > '$date'");

I need to select the date in my database when the date in database greater than today. My date format is dd-mm-yyyy , but I think because it's save in text, it only compares days(dd) which is wrong, any solution to solve this?

MySQL offers a STR_TO_DATE function to convert a date string to date:

SELECT * FROM reservation_db WHERE STR_TO_DATE(`date`) > '$date'

But as ankit suthar mentioned in above comment , it is not recommended to store dates as text.

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