Last active
June 22, 2020 20:30
-
-
Save czachor/f2b68c603356c5996b1188858edd29c4 to your computer and use it in GitHub Desktop.
PHP: get last business day
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 | |
/** | |
* @param bool $with_today | |
* @return DateTime Last business day | |
*/ | |
function lastBusinessDay($with_today = false): DateTime | |
{ | |
$date = new DateTime(); | |
if (!$with_today) { | |
$date->sub(new DateInterval('P1D')); // yesterday | |
} | |
$w = $date->format('w'); | |
if ($w >= 1 && $w <= 5) { // between Mon-Fri | |
return $date; | |
} | |
return new DateTime('last friday'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment