What are the best practices for handling date calculations in PHP, specifically when determining the month in a quarter?

When determining the month in a quarter in PHP, it's important to consider the starting month of the quarter and calculate accordingly. One approach is to use the date() function to extract the month from a given date and then determine the quarter based on that month. You can also create a custom function to handle the calculation more efficiently.

// Function to get the quarter of a given date
function getQuarter($date) {
    $month = date('n', strtotime($date));
    $quarter = ceil($month / 3);
    return $quarter;
}

// Example of how to use the function
$date = '2022-05-15';
$quarter = getQuarter($date);
echo "The month in quarter for $date is Q$quarter.";