Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Date() to calculate three working days ago?

Tags:

php

I wish to get a PHP date for three working days ago.

I've found numerous examples for getting dates by all sorts of textual methods, but the closest I found was this one, but it returns three dates (not one date, three days ago) and requires a get_holidays function for which the code was not provided.

How can I write the PHP code to return three working days before today?

This works, but doesn't account for week/weekend days:

date('Y-m-d', strtotime('-3 days')); // returns 2012-12-01

This doesn't work, but is what I would like to see:

date('Y-m-d', strtotime('four week days ago'));

In fact, the above returns '1969-12-31'. As does this: strtotime('-4 week days').

like image 889
crashwap Avatar asked Dec 05 '12 00:12

crashwap


1 Answers

You can just keep going back a day until you get what you desire. I'm not sure this is the most efficient way, but it gets the job done:

$count = 0
$day = strtotime('-1 day');
while ($count < 3 || date('N', $day) > 5) {
   $count++;
   $day = strtotime('-1 day', $day);
}
like image 82
Explosion Pills Avatar answered Oct 08 '22 14:10

Explosion Pills