What is the correct syntax for comparing DateTime fields in MySQL queries when handling date ranges in PHP?

When comparing DateTime fields in MySQL queries to handle date ranges in PHP, it is important to use the correct syntax to ensure accurate comparisons. One common mistake is not using the proper format for DateTime values when comparing them in the query. To solve this issue, make sure to format the DateTime values using the `Y-m-d H:i:s` format before comparing them in the query.

// Example code snippet to compare DateTime fields in MySQL queries for date ranges in PHP
$start_date = '2022-01-01 00:00:00';
$end_date = '2022-01-31 23:59:59';

$query = "SELECT * FROM table_name WHERE date_field BETWEEN '$start_date' AND '$end_date'";
// Execute the query and fetch results