1

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

Faili
  • 980
  • 5
  • 14
  • 27

2 Answers2

3

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;
Narcis Radu
  • 2,519
  • 22
  • 33
  • That was quick. Thanks to all. – Faili Jul 19 '10 at 12:59
  • You are welcome. I found that piece of code once and I saved it into my library. Anyway, I will never use it, since there are better ways to do it. Just take a look at http://stackoverflow.com/questions/336127/calculate-business-days for a much better approach. – Narcis Radu Jul 19 '10 at 13:17
  • 1
    hmm, would that have failed on December 31st, 2008. 23:59:60? (Yes, it existed). That time + 86400 would have been January 2nd. An edge case if I ever saw one, nonetheless... – Wrikken Jul 19 '10 at 14:05
  • @Wrikken - interesting. Anyway, your edge case is somehow strange and the probability to replicate is extremely rare. – Narcis Radu Jul 19 '10 at 20:49
0

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.

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89