[PHP] Re: Calendar math .... how to count in week days....?

2004-05-14 Thread Anthony
The PEAR functions didn't seem to do what I was looking for.  I built this
function based on an idea given to me by  Daryl Meese.

After much trial and error, it actualy does what I need.  My function is
bassed on hours but you can easily change that.  It will add or subtract
days.
- Anthony

/* buisinessDays
 * Calculates a new date bassed on buisiness hours
 * vars: hours to adjust by, timestamp
 * returns: adjusted timestamp
 */
 function businessDays($hours,$date) {
  // this function assumes $date is a business day

  $dayHours=7.5; // set how many hours are in a day

  // set the days we need to adjust by.  there is a one day buffer
  $days=(abs($hours/$dayHours))+1;

  // weeks
  $adjust=604800 * floor($days / 5);

  // how many days are left over
  $remain=round($days % 5);

  // check for negative
  if ($hours0) {
   // check to see if we will land on a weekend, and make appropriate
adjustment
   if (date(w,$date)=$remain) {
$remain +=2;
   }
   // make the time adjustment, 8640 microseconds in a day, times the
remaining adjustment
   $date -=($adjust +(86400 * $remain));
  }
  else {
   // check to see if we will land on a weekend, and make appropriate
adjustment
   if ((6-date(w,$date))=$remain) {
$remain+=2;
   }
   // make the time adjustment, 8640 microseconds in a day, times the
remaining adjustment
   $date +=($adjust +(86400 * $remain));
  }
  return $date;
 }

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Calendar math .... how to count in week days....?

2004-05-06 Thread Torsten Roehr
Anthony [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 I've run into a bit of a problem, that I know has been dealt with before,
 but I don't know how to deal with it.  Very simple, I have an application
 that needs to do calendar math and do it specifically on business days.  I
 need to be able to add and subtract a number of days to only Monday though
 Friday.  Seems simple enough, but I can't figure out how to do it.  The
only
 think I've come up with so far is to build a function that counts every
day
 and checks to see if it's a weekend or not.  This wouldn't be all that
hard
 to do, but I'm wondering if there is a better way to do it?  Anyone have a
 really efficient function to do this, I need to figure out a LOT of dates
in
 this app.

 - Anthony

Hi Anthony,

take a look at PEAR's Calendar package:
http://pear.php.net/package/Calendar

There is a simple example for building a calendar. Maybe you can adapt it by
ignoring weekends:
http://pear.php.net/manual/en/package.datetime.calendar.intro-inahurry.php

Regards, Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php