在我的自定义表格中,我希望从表格中获取用户的差异为15天。 Date_diff函数不能正常工作?

 <?php
    global $wpdb;

    $today =date("Y-m-d H:i:s");

    foreach( $wpdb->get_results("SELECT * FROM tablename WHERE DATEDIFF($today,end_date)<=15 ") as $key => $row){
     // each column in your row will be accessible like this
        echo $my_column = $row->user_id;
        echo"<br/><br/>";
    }

?>

I have create a function in which i want the where the user registration ending date is coming to close to 15 days so i want the value in foreach loop. I have got some success. In my custom table i am getting value as the date in this format -> 2016-06-07 12:53:55 So i am printing the value $today variable in same format like to get the difference but it is printing nothing but if i remove where clause, I am able to get the values then. I dont know what i am doing wrong here. All i want is when it comes close to the 15 days before this date like 07 june 2016 so i will get those user ids which are going to expire. Please Can you help me I will definitely appreciate it.

  1. Use curdate() as a built in function of mysql, so you dont need to worry about passing a string into the query.
  2. switch datediff order to get the expected result.
  3. Add a filter to remove the ones that already expired.
SELECT * FROM tablename 
WHERE DATEDIFF(end_date,curdate())<=15 
and end_date>=curdate()

Try this query

SELECT * FROM tablename WHERE end_date >= DATE_SUB(NOW(), INTERVAL 15 DAY)

NOW() is a mysql function which return the current date and time