Created
October 30, 2024 01:23
-
-
Save vfontjr/0bb9b4c93adf8aa92922297e92f153b7 to your computer and use it in GitHub Desktop.
getLastFridayOfMonth for Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getLastFridayOfMonth($year, $month) { | |
// Start with the last day of the month | |
$lastDay = new DateTime("$year-$month-01"); | |
$lastDay->modify('last day of this month'); | |
// Find the last Friday before or on the last day of the month | |
if ($lastDay->format('N') < 5) { | |
// If the last day is before Friday, subtract days to reach the last Friday | |
$lastDay->modify('last Friday'); | |
} elseif ($lastDay->format('N') > 5) { | |
// If the last day is after Friday, also move it to the last Friday | |
$lastDay->modify('last Friday'); | |
} | |
return $lastDay->format('Y-m-d'); | |
} | |
// Example usage | |
echo getLastFridayOfMonth(2024, 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment