PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

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 Dev Shed Forums Sponsor:
  #1  
Old August 11th, 2005, 04:11 AM
xilviu xilviu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Bucharest
Posts: 46 xilviu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 58 m 12 sec
Reputation Power: 4
Send a message via Yahoo to xilviu
week days between 2 dates

i searched this forum for an answer and i found several topics similar to this one, but to be onest with you, they dont work, or they're made for weekends.

can you help me, please, as my project is standing still for 1 week now?
how DO you get the numbar of working days between 2 dates?

ps:working days in a week are from Monday to Friday(no=5)

thx for the interest

Reply With Quote
  #2  
Old August 11th, 2005, 04:45 AM
roninblade's Avatar
roninblade roninblade is offline
// no comment
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2001
Posts: 1,639 roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 45 m
Reputation Power: 34
here, give this a try.
PHP Code:
 $cur_date $date1 '2005-04-10';
$date2 '2005-07-10';
$week_day 0;

while (
$cur_date <= $date2)
{
    
$d explode('-'$cur_date);
    
$day date('w'mktime(0,0,0,$d[1],$d[2],$d[0]));
    if (
$day <= && $day >= 1$week_day++;
    echo 
'Current Date -> '.$cur_date.' : '.$week_day.'<br/>';
    
$cur_date date('Y-m-d'mktime(0,0,0,$d[1],$d[2]+1,$d[0]));
}
echo 
'<hr/>Total Weekdays from '.$date1.' to '.$date2.' is '.$week_day.'<br/>'

Last edited by roninblade : August 11th, 2005 at 04:54 AM.

Reply With Quote
  #3  
Old August 11th, 2005, 05:01 AM
briancaspe briancaspe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 20 briancaspe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 54 m 29 sec
Reputation Power: 0
Post

For the above to really work, you'd have to add tests to make sure the month and the year increase as you get later in date and also so you don't increase the days past the last day of the month. another way would be to do something like:

PHP Code:
 $date1 '2005-04-10';
  
$date2 '2005-07-10';
  
$week_day 0;
 
$d1 explode('-'$date1);
 
$date1_ts mktime(0,0,0,$d1[1],$d1[2],$d1[0]);
$d2 explode('-'$date2);
 
$date2_ts mktime(0,0,0,$d2[1],$d2[2],$d2[0]);

  while (
$date1_ts <= $date2_ts)
  {
    echo 
'Current Date -> '.date('Y-m-d',$date1_ts).'<br/>';
    
$day date('w'$date1_ts);
    if (
$day <= && $day >= 1$week_day++;
    
$date1_ts strtotime("+1 day"$date1_ts));
  }
  echo 
'<hr/>Total Weekdays from '.$date1.' to '.$date2.' is '.$week_day.'<br/>'

Reply With Quote
  #4  
Old August 11th, 2005, 05:02 AM
roninblade's Avatar
roninblade roninblade is offline
// no comment
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2001
Posts: 1,639 roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 45 m
Reputation Power: 34
did you at least try the code? the month and year increments is already taken care of by mktime in this line ...
PHP Code:
 $cur_date date('Y-m-d'mktime(0,0,0,$d[1],$d[2]+1,$d[0])); 

you're only doing it longer. no need to modify the code

Reply With Quote
  #5  
Old August 11th, 2005, 05:09 AM
briancaspe briancaspe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 20 briancaspe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 54 m 29 sec
Reputation Power: 0
didn't try the code. As I understood it, you are only increasing the day. I'm not sure how mktime acts when the days get higher than the number of days in the month. If it moves to the first day in the next month, then I've learned something new!

Reply With Quote
  #6  
Old August 11th, 2005, 05:18 AM
briancaspe briancaspe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 20 briancaspe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 54 m 29 sec
Reputation Power: 0
ok, I've tried it out. You're absolutely right.

Reply With Quote
  #7  
Old August 11th, 2005, 05:25 AM
xilviu xilviu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Bucharest
Posts: 46 xilviu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 58 m 12 sec
Reputation Power: 4
Send a message via Yahoo to xilviu
roninblade is right. (as in the manual)
i was working with a 'for' syntax to go day by day from date1 to date2, also using mktime to leap from one day to other, but for some reason didnt seem to act the right way.

i tried this script and seems to block my pc for some reason.
i entered:
$cur_date = $date1 = '2005-08-11';
$date2 = '2006-08-27';

and gave me this: Maximum execution time of 30 seconds exceeded in...... for this line: $day = date('w', mktime(0,0,0,$d[1],$d[2],$d[0]));

Reply With Quote
  #8  
Old August 11th, 2005, 05:38 AM
roninblade's Avatar
roninblade roninblade is offline
// no comment
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2001
Posts: 1,639 roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 45 m
Reputation Power: 34
errr... maybe you didn't advance the date by one and got caught in an endless loop so the script timed out.

Reply With Quote
  #9  
Old August 11th, 2005, 06:07 AM
xilviu xilviu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Bucharest
Posts: 46 xilviu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 58 m 12 sec
Reputation Power: 4
Send a message via Yahoo to xilviu
have any ideea why? i mean...i used your code and is perfect for dates like several months apart, but breakes when you try more then 11 months.

Reply With Quote
  #10  
Old August 11th, 2005, 06:34 AM
xilviu xilviu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Bucharest
Posts: 46 xilviu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 58 m 12 sec
Reputation Power: 4
Send a message via Yahoo to xilviu
this is the right way to do it.

PHP Code:
<?
function CalcWorkingDays ($start$end) {
  
$starttime gmmktime (000substr ($start52), substr ($start82), substr ($start04));
  
$endtime gmmktime (000substr ($end52), substr ($end82), substr ($end04));
  
$days = (($endtime $starttime) / 86400) + 1;
  
$d $days 7;
  
$w date("w"$starttime);
  
$result floor ($days 7) * 5;
  
$result $result $d - (($d $w) >= 7) - (($d $w) >= 8) - ($w == 0);
  return 
$result;
}

$date1 '2005-08-11';
$date2 '2006-12-11';

echo 
CalcWorkingDays($date1,$date2);

?>


thx guys for your time.

result found on: http://ro.php.net/manual/en/function.date.php

Reply With Quote
  #11  
Old August 11th, 2005, 08:16 AM
roninblade's Avatar
roninblade roninblade is offline
// no comment
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2001
Posts: 1,639 roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 45 m
Reputation Power: 34
Quote:
Originally Posted by xilviu
have any ideea why? i mean...i used your code and is perfect for dates like several months apart, but breakes when you try more then 11 months.


dont know why its doing that for you but i tried two dates several years apart and it worked fine here.

Reply With Quote
  #12  
Old August 11th, 2005, 10:00 AM
FlyingSmurf FlyingSmurf is offline
The Freshmaker
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Smurf Village
Posts: 335 FlyingSmurf Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 Days 21 h 38 m 32 sec
Reputation Power: 0
Hmmm ... the version you posted seems a bit confusing to me xilviu, and I don't like the use of floor() ... although it does seem to work.

Here's how I would write it

PHP Code:
 $date1 '2005-08-11';
$date2 '2006-04-06';
$week_day 0;

$diff_days = (strtotime($date2) - strtotime($date1)) / (3600 24);
$left_over $diff_days 7;

// if diff_days is not a factor of 7 calculate how many working days there are
// in the incomplete week
if($left_over)
{
    
$d explode('-',$date1);
    
    for(
$x 0$x <= $left_over$x++)
    {
        
$day date('w',mktime(0,0,0,$d[1],$d[2]+$x,$d[0]));
        if(
$day && $day 6$week_day++;
    }
    
    
$diff_days -= $left_over// now diff_days is a factor of 7
}

$week_day += ($diff_days 7) * 5// 5 working days per week
print $week_day

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > week days between 2 dates