How can you optimize PHP code for querying dates in a MySQL database efficiently?

When querying dates in a MySQL database efficiently, it is important to use the proper date and time functions provided by MySQL, such as DATE_FORMAT and DATE_SUB. Additionally, using indexes on date columns can greatly improve query performance. It is also recommended to avoid using PHP date manipulation functions within the query itself, as this can be less efficient.

// Example code snippet for querying dates in a MySQL database efficiently
$startDate = '2022-01-01';
$endDate = '2022-12-31';

$sql = "SELECT * FROM table_name WHERE date_column BETWEEN '$startDate' AND '$endDate'";
// Execute the query and fetch results