How to check if a date is between date1 and date2 using mysql?

前端 未结 6 2117
醉梦人生
醉梦人生 2021-01-03 03:49

I\'m trying to write a query that will check today\'s date against my table columns date1 and date2 in mysql/php.. This is what I\'m after:

\'events\' table:

6条回答
  •  臣服心动
    2021-01-03 04:41

    Modified version of styfle's code

    $date1 = '2010-01-01'; // example min
    $date2 = '2015-01-01'; // example max
    $sql = "SELECT * FROM events WHERE $date1 >= '2012-01-18' AND $date2 <= '2012-01-18';";
    $result = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($result) != 0) {
        // This date is equal to or within the range specified
    } else {
        // The date was not within the range specified
    }
    

    then you can have code executed based on the result

提交回复
热议问题