Skip to content

Instantly share code, notes, and snippets.

@czachor
Last active June 22, 2020 20:30
Show Gist options
  • Save czachor/f2b68c603356c5996b1188858edd29c4 to your computer and use it in GitHub Desktop.
Save czachor/f2b68c603356c5996b1188858edd29c4 to your computer and use it in GitHub Desktop.
PHP: get last business day
<?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