What are some best practices for using the DateTime class in PHP to filter dates within a specific range?

When filtering dates within a specific range using the DateTime class in PHP, it's important to set the start and end dates, create DateTime objects for comparison, and then check if a given date falls within the specified range.

// Set start and end dates
$start_date = '2022-01-01';
$end_date = '2022-12-31';

// Create DateTime objects for comparison
$start_datetime = new DateTime($start_date);
$end_datetime = new DateTime($end_date);

// Check if a given date falls within the specified range
$given_date = '2022-06-15';
$given_datetime = new DateTime($given_date);

if ($given_datetime >= $start_datetime && $given_datetime <= $end_datetime) {
    echo "Given date is within the specified range.";
} else {
    echo "Given date is outside the specified range.";
}