PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Old April 2nd, 2008, 09:54 AM
matthewst matthewst is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 136 matthewst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 22 h 22 m 19 sec
Reputation Power: 12
How do I calculate the number of WORKING days from a given date

I need to calculate 14 working days after a given date.
I found this on php.net:

Code:
<?php
$holidayList = array("04-03-2008","07-03-2008");
$j = $i = 1;
///changed to 14
while($i <= 5)
///it didn't work
{
    $day = strftime("%A",strtotime("+$j day"));
    $tmp = strftime("%d-%m-%Y",strtotime("+$j day"));
    if($day != "Sunday" and $day != "Saturday" and !in_array($tmp, $holidayList))
    {
        $i = $i + 1;
        $j = $j + 1;
    }
    else
        $j = $j + 1;
}
    $j = $j -1;
echo strftime("%A, %d-%m-%Y",strtotime("+$j day"));
?>


The author designed it to calculate 5 working days. When I change the 5 to 14 it calculates 22 working days.

Reply With Quote
Old April 2nd, 2008, 10:30 AM
DavidMR's Avatar
DavidMR DavidMR is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Galway
Posts: 1,437 DavidMR User rank is Lance Corporal (50 - 100 Reputation Level)DavidMR User rank is Lance Corporal (50 - 100 Reputation Level)DavidMR User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Month 3 Days 20 h 56 m 48 sec
Reputation Power: 13
PHP Code:
//call with: MM/DD/YYYY
echo getWorkingDays("02/04/2007","12/04/2007");
<?
php
function getWorkingDays($startDate$endDate$holidays){

    
// Calculate weekday number. Monday is 1, Sunday is 7
    
$firstWeekdayNumber date("N"strtotime($startDate));
    
$lastWeekdayNumber  date("N"strtotime($endDate));

    
// Normalize the dates if they're weekends or holidays as they count for full days (24 hours)
    
if ($firstWeekdayNumber == || $firstWeekdayNumber == || in_arraydate("Y-m-d"strtotime($startDate)), $holidays ))
        
$startDate date("Y-m-d 00:00:00"strtotime($startDate));
    if (
$lastWeekdayNumber == 6  || $lastWeekdayNumber == 7  || in_arraydate("Y-m-d"strtotime($endDate)), $holidays ))
        
$endDate   date("Y-m-d 00:00:00"strtotime("+1 days"strtotime$endDate )));

    
// Compute the floating-point differences in the dates
    
$daysDifference          = (strtotime($endDate) - strtotime($startDate)) / 86400;
    
$fullWeeksDifference     floor($daysDifference 7);
    
$remainingDaysDifference fmod($daysDifference7);

    
// Subtract the weekends; In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
    
if ($firstWeekdayNumber <= $lastWeekdayNumber){
        if (
$firstWeekdayNumber <= && <= $lastWeekdayNumber && $remainingDaysDifference >= 1$remainingDaysDifference--;
        if (
$firstWeekdayNumber <= && <= $lastWeekdayNumber && $remainingDaysDifference >= 1$remainingDaysDifference--;
    }
    else{
        if (
$firstWeekdayNumber <= 6  && $remainingDaysDifference >= 1$remainingDaysDifference--;
        
// In the case when the interval falls in two weeks, there will be a Sunday for sure
        
$remainingDaysDifference--;
    }

    
// Compute the working days based on full weeks +
    
$workingDays $fullWeeksDifference 5;
    if (
$remainingDaysDifference )
        
$workingDays += $remainingDaysDifference;

    
// Subtract the holidays
    
foreach($holidays as $holiday)
    {
        
$holidayTimeStamp=strtotime($holiday);
        
// If the holiday doesn't fall in weekend
        
if (strtotime($startDate) <= $holidayTimeStamp && $holidayTimeStamp <= strtotime($endDate) && date("N",$holidayTimeStamp) != && date("N",$holidayTimeStamp) != && $workingDays >= 1)
            
$workingDays--;
    }
   
    
// End of calculation, return the result now
    
return $workingDays;
}
?> 
__________________
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.

Last edited by DavidMR : April 2nd, 2008 at 10:40 AM.

Reply With Quote
Old April 2nd, 2008, 12:23 PM
matthewst matthewst is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 136 matthewst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 22 h 22 m 19 sec
Reputation Power: 12
Thanks but that will get me the number of working days.
I need to know the date 14 working days after a given date?
Lets say the given date is 3/1/2008.
It should calculate 3/20/2008.
Mine keeps going to 4/23/2008.

I'm using this to get the given date into the equation:
Code:
<?php
$holidayList = array("04-03-2008","07-03-2008");
$j = $i = 1;
///changed to 14
while($i <= 5)
///it didn't work
{
$day = $given_date;      
$day = strftime("%A",strtotime("+$j day"));
// then the rest of the code

Reply With Quote
Old April 2nd, 2008, 01:40 PM
matthewst matthewst is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 136 matthewst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 22 h 22 m 19 sec
Reputation Power: 12
Getting closer.

Code:
$given_date = "3/1/2008";
$day = strftime("%A",strtotime("+$j $given_date"));


gets me 4/16/2008

Echoing $day = Friday
3/1/2008 was a Saturday

I changed the last bit of code to this.
Code:
$working_days = strftime("%A, %d-%m-%Y",strtotime("+$j day"));

Echoing $working_days = 04/16/2008

Reply With Quote
Old April 2nd, 2008, 02:35 PM
DavidMR's Avatar
DavidMR DavidMR is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Galway
Posts: 1,437 DavidMR User rank is Lance Corporal (50 - 100 Reputation Level)DavidMR User rank is Lance Corporal (50 - 100 Reputation Level)DavidMR User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Month 3 Days 20 h 56 m 48 sec
Reputation Power: 13
when the function is loaded:

$difference = getWorkingDays("02/04/2007","12/04/2007");

(with the date format you use, $difference will be an INT with teh number of weekdays...

Reply With Quote
Old April 3rd, 2008, 01:28 PM
matthewst matthewst is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 136 matthewst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 22 h 22 m 19 sec
Reputation Power: 12
Code:
<?php
$holidayList = array("04-03-2008","07-03-2008");
$j = $i = 0; //changed this to 0 or you were always starting one day ahead
$given_date = "3/1/2008";
$tmp1 = strtotime($given_date); //worked out a timstamp to start with
while($i < 14)
{
$tmp2 = strtotime("+$j day", $tmp1);
$day = strftime("%A",$tmp2);
echo strftime("%d-%m-%Y",$tmp2);

    $tmp = strftime("%d-%m-%Y",$tmp2);
    if(($day != "Sunday") && ($day != "Saturday" )&&(!in_array($tmp, $holidayList)))
    {
        $i = $i + 1;
        $j = $j + 1;
        
    }
    else
    {
        $j = $j + 1;
       
    }
}
    $j = $j -1;
$newdate = strtotime("+$j day",$tmp1);
$working_days = strftime("%A, %d-%m-%Y",$newdate);
?>


Works perfectly!! Thanks!

Reply With Quote
Old April 3rd, 2008, 01:43 PM
DavidMR's Avatar
DavidMR DavidMR is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Galway
Posts: 1,437 DavidMR User rank is Lance Corporal (50 - 100 Reputation Level)DavidMR User rank is Lance Corporal (50 - 100 Reputation Level)DavidMR User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Month 3 Days 20 h 56 m 48 sec
Reputation Power: 13
glad i could help

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > How do I calculate the number of WORKING days from a given date


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap