Skip to the next business day if the date falls on a weekend

Asked

Viewed 2,619 times

4

In PHP, I have a certain date, coming from the database, which is the date a request was made.

I need to add 5 working days on that date, which is the deadline for the request to be delivered. In this count I need to skip the weekends.

I currently have the following code to add 5 days, but the part about skipping the weekends is that it’s been hard.

Example:

 $data_banco = new DateTime('2016-07-07'); // Quinta

 $data_prazo = clone $data_banco;

 $data_prazo->modify('+5 days'); 

What’s the simplest way to add dates, skipping the weekend, in PHP?

1 answer

8


As I say sometimes, some PHP functions are "speakers" (in PHP this is called relative formats), in case there is the instruction called weekday, representing days of the week (or as we call it in Portuguese, working days)

If you do it add up any day:

$obj->modify('+5 days');

If you do this it will add up days of the week that are the "working days":

$obj->modify('+5 weekdays');

Documentation for other instructions/formats (Relative Formats/Relative formats):

  • 3

    Related http://answall.com/a/50716/70

  • 1

    These "speaking" functions, I just discovered that the documentation calls "relative formats", ie relative formatting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.