Last Updated: February 25, 2016
·
9.289K
· zuul

Get the previous working day

Getting yesterday's date using the PHP date() function is simple:

<?php
$lastWorkingDay = date( "d", strtotime("-1 day") );
?>

The problem began when the past day was Friday of the previous week.

To deal with this, just get the current day of the week, then switch between the collected value to go back the necessary number of days:

<?php

//get weekday, from 0 (sunday) to 6 (saturday)
$currentWeekDay = date( "w" );

switch ($currentWeekDay) {
  case "1": {  // monday
    $lastWorkingDay = date("d", strtotime("-3 day"));
    break;
  }
  case "0": {  // sunday
    $lastWorkingDay = date("d", strtotime("-2 day"));
    break;
  }
  default: {  //all other days
    $lastWorkingDay = date("d", strtotime("-1 day"));
    break;
  }
}
?>

2 Responses
Add your response

I'm not sure, but I believe that the working days depend on the country, don't they ?

Edit, as seen in the Google, in Dubai, the working days are from Sunday to Thursday. But it should correspond in most cases ;)

over 1 year ago ·

Just did this myself. Got it a bit shorter though =)

$lastWorkingDay = date( "d", strtotime("today -1 Weekday") );

Or if you need to include today too, like in my case:

$lastWorkingDay =(date("N")<=5)?date("Y-m-d"):date("Y-m-d",strtotime('today -1 Weekday'));

over 1 year ago ·