views:

286

answers:

3

Ello,

I am struggling with some date troubles here in PHP/MySql. I have a form that offers the recurrence of a payment yearly,quarterly,monthly,bi-weekly,or weekly and I need to be able to track when payments are made.

So for example, if something is added on Monday March 8th (that's the date it is being entered), recurring Weekly on Thursdays (the user would only see a dropdown offering days of the week -- as the recurrence is weekly), it would enter the 52 (weekly) due dates into the db (julian/whatever is easiest), and mark the first 9 of them paid (since those days are past); the rest entered but marked unpaid.

I have a table that would have each due_date entry, with a link to the payment detail, and a Boolean field paid/not paid.

So in the above example, the payments table would look something like:

id | pmnt_id | due_date | is_paid
1  |  0023   | 01/07/10 | 1
2  |  0023   | 01/14/10 | 1
3  |  0023   | 01/21/10 | 1
10 |  0023   | 03/25/10 | 0
11 |  0023   | 04/01/10 | 0

etc...

The problem is, well, everything. The calendaring system is so effed up, that there seems no real logical/straightforward way to do this. I've been racking my brain, searching goggle and php date manual all morning, and I'm just getting more confused.

Any ideas/suggestions/pointers would be greatly appreciated.

Cheers.


[snip]

$startDate = strtotime("$currentDay $currentMonthFull $currentYear");
$stTimeFormatted = strftime('%d/%m/%Y',$startDate);
$numPmnts = ($totalWeeks-$currentWeek);
for($i=0;$i<$numPmnts;$i++){
    $ddates=array(
    'sched_pay_id'=>$pmntID,
    'due_date'=>date('m/d/y', strtotime('+'.$i.' week', $startDate)),
    'is_paid'=>0,
    );
    $this->db->insert('sched_payments',$ddates);
}
+2  A: 

You might want to look at strtotime: http://php.net/manual/en/function.strtotime.php, you could loop over that until you reach the end of the period (in this case 52 weeks).

for i = 1, i <= 52, i++
    insert date(mm / dd / YY, strtotime(+ i week));
/for
SeanJA
A: 

The new PHP Date functions have an add method that you can use to generate all these dates.

$startDate = new DateTime();

// "Period of 1 Week
$interval = new DateInterval("P1W");

// Will cycle from 1st week's due date to the end of payment cycle
for($i = 1; $i <= 52; $i++) {
    $dueDate = $date->add($interval);
    $date = $dueDate;
}
Anurag
Thanks. I'm using Codeigniter, however, and it doesn't seem to want to play nice with `dateTime()`
stormdrain
A: 

This an example of recurrence days.

This function return a recurrence day per week during a period. Exemple: All Thurday starting from 06 May 2010 during 52 weeks.

- '.$result[$i]; } ?>

ESOWEB