# Any PHP/ Java Script similar function like WORKDAY() function in Excel???



## vincee123 (May 15, 2008)

I am rewriting an application in PHP that is existing in Excel now.

In excel we have this function called WORKDAY() which Returns a number that represents a date that is the indicated number of working days before or after a date (the starting date). Working days exclude weekends and any dates identified as holidays. 

Example

The example may be easier to understand if you copy it to a blank worksheet.

=WORKDAY('10/01/2008','151') 

Result:	Date 151 workdays from the start date (4/30/2009) 

Do we have a similar function or workaround in PHP or JavaScript that I could use? 

Any solutions would be of great help.


----------



## Jaxo (Feb 21, 2008)

vincee123,

Although I am VERY new to PHP I did some searching. Doesn't look like there is a built in PHP function to accomplish what you want. Everything I ran across online were custom built scripts. Nothing that really matches what your trying to do (but similar). Some did involve figuring out work days, but nothing to the extend of what your asking for.

Sorry can't be of any help. Sounds like it's going to have to be created.

Good luck and keep us posted please.


----------



## Redcore (Aug 14, 2007)

I've been messing around with some code for quite a few hours and it's pretty complicated. I was able to skip over weekend dates - but holidays are a whole different situation...at least, if you're going to make that dynamic. I don't have it in me to keep going


----------



## vincee123 (May 15, 2008)

Hi, even the code that would skip the week ends alone should do fine for me, I dont need the holidays ....


----------



## ShosMeister (Jan 19, 2008)

http://www.webmasterworld.com/php/3207166.htm


----------



## Redcore (Aug 14, 2007)

ShosMeister - I was referring to that earlier. If you had tested it, you would have seen that it doesn't work. It spits out a random number like "1219924800".

vincee123, here's what I had come up with:


```
function workday($date, $days, $start_today=1)
	{
	if(!$date || !$days)
		{
		// if there are no date or days variables entered, spit it out
		return "ERROR: You must enter a date AND time. Example: workday('10/01/2008', '151')";
		}
	else
		{
		if($start_today==2)
			{
			// if they don't want to start today, they should be entering 2
			// add another day to skip today
			$days = $days+1;
			}
		$i=0;
		$nonworkdays=0;
		while($i<$days)
			{
			$nextday = $i*24*60*60;
			$nextdate = date('m/d/Y',(strtotime($date))+$nextday);
			$weekday = date("D", strtotime($nextdate));
			
			// check memorial day
			if($nextday == $memorial_date)
				{
				// add a day to basically "skip" memorial day
				++$nonworkdays;
				}
			
			if($weekday=="Sat" || $weekday=="Sun")
				{
				// add a day to basically "skip" the weekend
				++$nonworkdays;
				}
			
			
			++$i;
			}
			
		// turn days into seconds: * days X 24 hours X 60 minutes X 60 seconds
		$days = $days+$nonworkdays;
		$offset = $days*24*60*60;
		return date('m/d/Y',(strtotime($date))+$offset);
		}
	}
```
To call this...

```
echo workday('5/20/2008', '7');
```
To skip the first day if that day is today (7 days from now)

```
echo workday('5/20/2008', '7', '2');
```


----------



## ShosMeister (Jan 19, 2008)

Sorry Red, just did a quick look at it and wasn't anywhere that I could test it.


----------

