What are some best practices for organizing and structuring PHP code, especially when dealing with date-related functions like determining Advent weeks?

When organizing and structuring PHP code for date-related functions like determining Advent weeks, it is best to create a separate function for this specific task. This function should take the current date as input and calculate the corresponding Advent week based on the date. By encapsulating this logic in a separate function, the code becomes more modular, easier to test, and maintainable.

function getAdventWeek($date) {
    $adventStart = new DateTime('last sunday of november ' . date('Y', strtotime($date)));
    $diff = (new DateTime($date))->diff($adventStart)->days;
    $adventWeek = ceil(($diff + 1) / 7);
    
    return $adventWeek;
}

// Example of how to use the function
$currentDate = '2022-12-03';
$adventWeek = getAdventWeek($currentDate);
echo 'Advent week for ' . $currentDate . ' is: ' . $adventWeek;