Faili
Faili

Reputation: 1000

Incrementing days if day is a workday

I need your help again. This should be a function for php. I've got two dates. One is set by myDate and the other one is the date of today. I want to find out the number of days left to myDate, but saturday and sundy should be excluded. The result for this function would be 7... How can I make it work?


<?php
myDate = "29.07.2010 "

DaysTillmyDate = 0 iterate day to myDate { if (date/day is a weekday(Monday,Tuesday,Wednesday,Thursday, Friday)) increment DaysTillmyDate by 1 } ?>

A hint or any help would be much appreciated. Faili

Upvotes: 1

Views: 787

Answers (2)

Narcis Radu
Narcis Radu

Reputation: 2547

Quick iteration:

$days = 0;
for($i = time(); $i < (strtotime('29.07.2010') + 86400); $i=$i+86400)
{
    $weekday = date('w', $i);
    if($weekday > 0 &&  $weekday < 6)
    {
       $days++; 
    }
}

echo $days;

Upvotes: 3

Victor Nicollet
Victor Nicollet

Reputation: 24577

date('N', $theDate) gives you the day of the week (6 for Saturday and 7 for Sunday). From there, you can easily implement a function that does only counts workdays. On the other hand, this does not detect holidays.

Upvotes: 0

Related Questions