<?php
// create a new instance of DateTimeImmutable
$date = new \DateTimeImmutable('2017-08-05');
// create a one-day interval
$interval = new \DateInterval('P1D');
// subtract the interval, and keep doing that while the day before is not a weekday
do {
$date = $date->sub($interval);
} while (5 < $date->format('N'));
echo $date->format('Y-m-d');
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
2017-08-04