-1

How can I get the last weekday (Mon-Fri) of the current month in php?

magpielark
  • 19
  • 5
  • Was this one too crypric? https://stackoverflow.com/q/38509093/2943403 or https://stackoverflow.com/q/13960133/2943403 – mickmackusa Sep 07 '21 at 09:04

1 Answers1

0

Using date() and strtotime().

strtotime("last weekday October 2021") will produce the timestamp of the last weekday of September. Using that, we just need to add a month to the current date and get the last weekday before that month using strtotime.

$nextMonth = strtotime("+1 month");
$dateString = "last weekday " . date("F Y", $nextMonth);
$lastWorkDayOfMonth = date("Y-m-d", strtotime($dateString));
magpielark
  • 19
  • 5
  • This does work, but can you explain why `strtotime("last weekday October 2021")` works for **September**? Anyone? – KIKO Software Sep 07 '21 at 09:03
  • I suppose "October 2021" get's translated to the first of October. "last weekday" finds the last weekday before the first of October. That equals the last weekday of September. – magpielark Sep 07 '21 at 13:11