Bluemagica
Bluemagica

Reputation: 5158

How do I find date prior to another date in php

I need to find date x such that it is n working days prior to date y.

I could use something like date("Y-m-d",$def_date." -5 days");, but in that case it wont take into consideration the weekend or off-date. Let's assume my working days would be Monday to Saturday, any idea how I can accomplish this?

Upvotes: 1

Views: 111

Answers (3)

SDC
SDC

Reputation: 14222

A quick bit of googling got me to this page, which includes a function for calculating the number of working days between two dates.

It should be fairly trivial to adjust that concept to suit your needs.

Your problem, however, is that the concept of "working days" being monday to friday is not universal. If your software is only ever being used in-house, then it's okay to make some assumptions, but if it's intended for use by third parties, then you can't assume that they'll have the same working week as you.

In addition, public holidays will throw a big spanner in the works, by removing arbitrary dates from various working weeks throughout the year.

If you want to cater for these, then the only sensible way of doing it is to store the dates of the year in a calendar (ie a big array), and mark them individually as working or non-working days. And if you're going to do that, then you may as well use the same mechanism for weekends too.

The down-side, of course, is that this would need to be kept up-to-date. But for weekends, at least, that would be trivial (loop through the calendar in advance and mark weekend days where date('w')==0 or date('w')==6).

Upvotes: 0

Bluemagica
Bluemagica

Reputation: 5158

Thanks for the help guys, but to solve this particular problem I wrote a simple code:

    $sh_padding = 5; //No of working days to count backwards 
    $temp_sh_padding = 1; //A temporary holder
    $end_stamp = strtotime(date("Y-m-d", strtotime($date_format)) . " -1 day"); //The date(timestamp) from which to count backwards
    $start_stamp = $end_stamp; //start from same as end day
    while($temp_sh_padding<$sh_padding)
    {
        $sh_day = date('w',$start_stamp);
        if($sh_day==0){ //Skip if sunday
        }
        else
        {
            $temp_sh_padding++;
        }
        $start_stamp = strtotime(date("Y-m-d",$start_stamp)." -1 day");
    }
    $sh_st_dte = date("Y-m-d",$start_stamp); //The required start day

Upvotes: 0

pckabeer
pckabeer

Reputation: 694

Try this

<?php
function businessdays($begin, $end) {
    $rbegin = is_string($begin) ? strtotime(strval($begin)) : $begin;
    $rend = is_string($end) ? strtotime(strval($end)) : $end;
    if ($rbegin < 0 || $rend < 0)
        return 0;

    $begin = workday($rbegin, TRUE);
    $end = workday($rend, FALSE);

    if ($end < $begin) {
        $end = $begin;
        $begin = $end;
    }

    $difftime = $end - $begin;
    $diffdays = floor($difftime / (24 * 60 * 60)) + 1;

    if ($diffdays < 7) {
        $abegin = getdate($rbegin);
        $aend = getdate($rend);
        if ($diffdays == 1 && ($astart['wday'] == 0 || $astart['wday'] == 6) && ($aend['wday'] == 0 || $aend['wday'] == 6))
            return 0;
        $abegin = getdate($begin);
        $aend = getdate($end);
        $weekends = ($aend['wday'] < $abegin['wday']) ? 1 : 0;
    } else
        $weekends = floor($diffdays / 7);
    return $diffdays - ($weekends * 2);
}

function workday($date, $begindate = TRUE) {
    $adate = getdate($date);
    $day = 24 * 60 * 60;
    if ($adate['wday'] == 0) // Sunday
        $date += $begindate ? $day : -($day * 2);
    return $date;
}

$def_date="";//define your date here
$preDay='5 days';//no of previous days  
date_sub($date, date_interval_create_from_date_string($preDay));
echo businessdays($date, $def_date); //date prior to another date 
?>

Modified from PHP.net

Upvotes: 1

Related Questions