What potential pitfalls should be considered when using datetime fields in PHP queries?

When using datetime fields in PHP queries, one potential pitfall to consider is that the format of the datetime value may not match the format expected by the database. To avoid this issue, it is important to format the datetime value consistently before including it in the query. Using PHP's `DateTime` class can help ensure that the datetime value is formatted correctly.

// Assuming $datetime is the datetime value to be included in the query
$datetime = new DateTime($datetime);
$formattedDatetime = $datetime->format('Y-m-d H:i:s');

// Use $formattedDatetime in the query
$query = "SELECT * FROM table WHERE datetime_field = '$formattedDatetime'";