Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php date question

Status
Not open for further replies.

axman505

Technical User
Joined
Jun 20, 2001
Messages
489
Location
US
Hello,

everyime i load a certain page, i want to be able to calulate a date that is exactly 2 weeks from today, excluding weekends. Is there any easy way to accomplish this?

Or does anyone have prewritten code?

Thanks,
 
Code:
$twoweeks = date("M-d-Y",mktime(0,0,0, date("m"), date("d")+14, date("Y")));
echo $twoweeks."<br>";

How do you mean excluding the weekend?
Do you mean that if it is a Saturday or Sunday it should correct the date to dispay the coming Monday?

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
You can also use the strtotime() function:
Code:
$twoweeks = date("M-d-Y",strtotime("+2 weeks"));

Ken
 
I'm assuming that you're wanting x number of days from a valid list of days to count.

Code:
<?php

function add_specified_days ($start_date, $number_of_days, $valid_days)
{
        $days_of_week = array (1, 2, 4, 8, 16, 32, 64);
        $counter = 0;
        $test_date = strtotime($start_date);

        while ($counter < $number_of_days)
        {
                $test_date += 86400;

                $dow = $days_of_week[date ('w', $test_date)];

                if ($dow & $valid_days)
                {
                        $counter++;
                }
        }

        return date('Y-m-d', $test_date);
}

// Calculate the third parameter ($valid_days) by bitwize-oring
// (or adding -- the two operations are equivalent on this
// data) numbers from this table:
//
// su  m   t   w   th  f   sa
// 1   2   4   8   16  32  64
//
// If you want to count only weekdays, use 62
// If you want to count only weekends, use 65
// If you want to count only Sundays, use 64


$a = '2004-07-01';

//14 weekdays later
print add_specified_days($a, 14, 62);

print "\n";

//14 weekend days later
print add_specified_days($a, 14, 65);

print "\n";

//a month of Sundays later
print add_specified_days($a, 28, 64);

print "\n";
?>


This returns:
Code:
2004-07-21
2004-08-15
2005-01-08

Which seems correct by my hand-calculations.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Whow! That looks great (as per usual). Whish I could somehow store these threads!

Thanks m8

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top