NYCPHP Meetup

NYPHP.org

[nycphp-talk] Recurring Monthly Calculations

Kshitij Bedi Kbedi at inta.org
Fri Jun 25 12:39:00 EDT 2004


This is something I wrote for recurring events
You can choose a date from when the event starts to occur
($month,$day,$year)
and then till when to run($month2,$day2,$year2)
It can run for daily weekly biweekly or monthly.

I hope it helps you out

----------------------------------------------------------------------------
-----------------

//$repeat day is 1 through 7 for each day of the week starting from Monday
you want the even to occur

//$occurence=1 (Daily) 
//=7(weekly) = 
//14(biweekly) 
//=28 (Monthly)

//skip saturday and sunday
$arrskipdays=array(0,6);

//$month,$day,$year is when the events starts to occur and
$month2,$day2,$year2 is until when
//The loop will run for each day of the week requested which is stored in
$repeatday.

foreach ($repeatday as $value)
	{
		//Difference between today and the day the event should
start from.
		$daydiff=$value - intval(date("w",mktime (0,0,0,$month,
$day, $year)));
		
		//if day difference is less than zero than that means that
the day should start from next week.
		if ($daydiff<0) $daydiff+=7;
		
		$counter=0;
		
		//occurring weekly biweekly or monthly.
		while($counter<=$diff) 
		{
			//Only insert if resulting date is less or equal to
the final date.
			if (mktime (0,0,0,$month, ($day + $daydiff +
$counter),  $year)<=mktime(0,0,0,$month2,$day2,$year2))
			{
				if(!in_array(intval(date("w",mktime
(0,0,0,$month, ($day + $counter),  $year))),$arrskipdays))
				{
					echo
date("Y-m-d",mktime(0,0,0,$month, ($day + $daydiff + $counter),  $year));
					$counter=$counter + $occurrence;
				}
			}
		}	
	}

-----Original Message-----
From: Hans Zaunere [mailto:hans not junk at nyphp.com]
Sent: Friday, June 25, 2004 12:11 PM
To: NYPHP Talk
Subject: [nycphp-talk] Recurring Monthly Calculations



Hey all,

I needed to write a function that will tell me the dates of future
events that recurr monthly by day of month.  So for instance, "4th
Tuesday of the month for the next 6 months"

It ended up to be more difficult than I thought and I wanted to run the
function past you guys.  See attached.  Am I missing some clever way of
doing this?  The function seems to work, but another set of eyes would
be helpful - and it depends on UNIX timestamps, so...

The function is called as:

recurMonthlyByDay(2,4,20030522,23);

Which would return an array listing the 4th Tuesday, starting in May of
2003, for the next 23 months.  If you specify the second argument to be
greater than 5, then the last Tuesday would always be returned.


Thanks guys... feel free to use the function, too (that is, if it works)

H

 << File: RecurMonthlyByDay.inc >>  << File: ATT141846.txt >> 



More information about the talk mailing list