database – SELECT rows between two datetimes when the range is dynamic

I’m trying to select rows that fall into a certain date range from my database table. The range is determined dynamically by the user.

In my database I have a column called “dateAndTime”. It has dates stored into it in this format:

2021-06-01 19:37:00

I’m trying to select the rows with this:

function getEventsByDateRange(){

    $eventType = ($_POST("event"));
    $dateRangeFrom = ($_POST("dateRangeFrom"));
    $dateRangeTo = ($_POST("dateRangeTo"));

    $formattedDateRangeFrom = date("Y-m-d H:i:s", strtotime($dateRangeFrom));
    $formattedDateRangeTo = date("Y-m-d H:i:s", strtotime($dateRangeTo));

    global $wpdb;

    $eventQueryResults = $wpdb->get_results("SELECT * FROM dailyevents 
    WHERE dateAndTime BETWEEN {$formattedDateRangeFrom} AND {$formattedDateRangeTo} ORDER BY dateAndTime DESC", ARRAY_A); 

    $jsonEncoded = json_encode($eventQueryResults, JSON_UNESCAPED_UNICODE);

    echo $jsonEncoded;

    exit();

}

But I keep getting this error:

<div id="error"><p class="wpdberror"><strong>WordPress database error:</strong> (You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near &#039;04:00:00 AND 2021-06-02 03:59:59 ORDER BY dateAndTime DESC&#039; at line 2)<br /><code>SELECT * FROM dailyevents 
    WHERE dateAndTime BETWEEN 2021-06-01 04:00:00 AND 2021-06-02 03:59:59 ORDER BY dateAndTime DESC</code></p></div>()