5

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').

halfer
  • 19,824
  • 17
  • 99
  • 186
crashwap
  • 2,846
  • 3
  • 28
  • 62
  • 2
    it returns 1969 because that is the default beginning of epoch. In other words - it doesn't know what you mean by trying to convert four week days ago into a timestamp. – Kai Qing Dec 05 '12 at 00:54
  • Your second piece of code doesn't work, because it's not a valid [relative time format](http://php.net/manual/en/datetime.formats.relative.php) – Daniel Miladinov Dec 05 '12 at 00:54
  • Similiar: http://stackoverflow.com/questions/3093266/find-three-previous-working-days-from-a-given-date – Lior Dec 05 '12 at 00:56
  • @Lior - I referenced that post in my question (see [this one]) and explained why it was not the right answer. – crashwap Dec 05 '12 at 00:58
  • @Lior: similar? pretty much the exact same lol... – anson Dec 05 '12 at 00:59
  • @cssyphus: your link is actually a different post from Lior's, look up his link and you'll find a good answer – anson Dec 05 '12 at 01:01
  • @andbeyond You're right, thank you. But I did see that answer before and I was going to reference in my post but decided to skip it (should have noted it). My reasons for not seeing that post as the answer were that it didn't appear to handle work days and it was concerned with file dates. I appreciate that's very similar, but I'm not quite at the level of being able to interpolate the two formats. – crashwap Dec 05 '12 at 01:11
  • See http://stackoverflow.com/a/4261223/3411766 strtotime ( '3 weekdays' ) – cottton Feb 17 '16 at 17:41

5 Answers5

11

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);
}
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

FINAL ANSWER:

For novices like me, this was the working code using Explosion Pills' answer:

function working_days_ago($days) {
    $count = 0;
    $day = strtotime('-1 day');
    while ($count < $days || date('N', $day) > 5) {
       $count++;
       $day = strtotime('-1 day', $day);
    }
    return date('Y-m-d', $day);
}

$three_days_ago = working_days_ago('3');

Works like a charm !

crashwap
  • 2,846
  • 3
  • 28
  • 62
0

I was getting some unexpected results from the above functions so I wrote this one. Arguments are number of days, forward (1) or backwards(0), and a date. If no date is supplied todays date will be used:

// returned $date Y/m/d
function work_days_from_date($days, $forward, $date=NULL) 
{
    if(!$date)
    {
        $date = date('Y-m-d'); // if no date given, use todays date
    }

    while ($days != 0) 
    {
        $forward == 1 ? $day = strtotime($date.' +1 day') : $day = strtotime($date.' -1 day');
        $date = date('Y-m-d',$day);
        if( date('N', strtotime($date)) <= 5) // if it's a weekday
        {
          $days--;
        }
    }
    return $date;
}
Angus
  • 141
  • 1
  • 2
  • 13
0

3 days ago from today:

$date = new DateTime();
$date->sub(new DateInterval('P3D'));
echo $date->format('Y-m-d') . "\n";

3 days ago from a particular date:

$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('P3D'));
echo $date->format('Y-m-d') . "\n";
itsazzad
  • 6,868
  • 7
  • 69
  • 89
-1

get day after function ↓

function working_days($days){
            $day = strtotime(date('Y-m-d'));
            for ($x=0;$x<$days;$x++) {
                $day = strtotime('+1 day', $day);
                if (date('N', $day) > 6) {
                    $x = $x - 1;
                }
            }
            return date('Y-m-d',$day);
        }
       print_r( working_days(25));
    }
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Aug 14 '18 at 11:46