PICKING MEMBERSHIP RENEWAL DATE AND DURATION - Apr 20th, 2015


Instead of calculating an expiration date (expiresDate) based on the date that a member signed up, an organization
wanted to calculate the expiration date and membership duration (in years) from a fixed month.

Further, they wanted anyone signing up after a particular cutoff month relative to the current month, to get an extra
year added to their membership, so that their initial membership period was never shorter than one year.

Some of the other challenges were calculating a valid date for months that had 30 or 28 days vs those that had 31 days,
and generating a valid MySQL date from the calculation.

With Claire Ryan's and Greg Thomas' from Interactive Tools help, we came up with the following solution.

In a single record editor (mine was called ‘Organization Information’) there are 2 list fields, a text field, and a
check box . The list fields are called Start Month and Cutoff Month. Their values are:

1|January
2|February
3|March
4|April
5|May
6|June
7|July
8|August
9|September
10|October
11|November
12|December

NOTE: the format number|text allows the list to display the text in the pull down menu, while inserting the selected
value in numbers.

The text field is called Renewal Duration and accepts 1 digit with a value from 1-9. With the description: Enter the
digit corresponding to the Renewal Duration in years (1-9).

The check box ( called ‘Valid for One Year From Date Joined’), with the description: “If renewal period begins on
date joined, check this box. Otherwise the 3 fields below will determine the renewal criteria.”

Then define a few variables:

<?php
@$curr_month = date("n") ;
@
$start_month = str_pad(@$organization_informationRecord['start_month'], 2, "0", STR_PAD_LEFT);
@
$cutoff_month = @$organization_informationRecord['cutoff_month'] ;
@
$renewal_duration = @$organization_informationRecord['renewal_duration'] ;
@
$currentYear = date('Y');
?>

After that, perform the calculations required and check for valid last day of the month :

<?php
@$renewalYear = (intval(@$curr_month) >= intval(@$cutoff_month))? @$currentYear + (@$renewal_duration) :@ $currentYear +
(@
$renewal_duration - 1) ;
@
$expiryMonth = ($organization_informationRecord['start_month'] -1);
if(@
$expiryMonth < 10) {
@
$expiryMonth = "0".@$expiryMonth;
}
if(@
$expiryMonth == 00) {
@
$expiryMonth = 12;
}
@
$testEndDate = @$renewalYear. '-'.@$expiryMonth.'-01';
@
$lastDayofMonth = date('t', strtotime(@$testEndDate));
if(@
$expiryMonth == 02) {
@
$lastDayofMonth = 28;
}
if(@
$expiryMonth == 04 || @$expiryMonth == 06 || @$expiryMonth == 09 || @$expiryMonth == 11) {
@
$lastDayofMonth = 30;
}
if(@
$expiryMonth == 01 || @$expiryMonth == 03 || @$expiryMonth == 05 || @$expiryMonth == 07 || @$expiryMonth == 08 ||
@
$expiryMonth == 10 || @$expiryMonth == 12 ) {
@
$lastDayofMonth = 31;
}
?>

And then create a valid MySQL date from the result:

<?php $adjustedMembershipExpiryDate = $renewalYear. '-'.$expiryMonth.'-'.$lastDayofMonth.' 00:00:00'; ?>


You can use the following to test your progress.

Current Month is: <?php echo date("n"); ?><br />
Test End Date is: <?php echo @$testEndDate ; ?><br />
Start Month is: <?php echo @$start_month ; ?><br />
Expiry Month is: <?php echo @$expiryMonth ; ?><br />
Last Day of Month: <?php echo @$lastDayofMonth ?><br />
Cutoff Month is: <?php echo @$cutoff_month ?><br />
Renewal Duration is:<?php echo @$organization_informationRecord['renewal_duration'] ?> years<br />
Current Year is: <?php echo date('Y'); ?><br />
Renewal Year is: <?php echo @$currentYear + (@$renewal_duration - 1); ?><br />
Calculated $adjustedMembershipExpiryDate is: <?php echo @$adjustedMembershipExpiryDate ?> <br />

<?php if(@$curr_month >= @$cutoff_month): ?>
It's <?php echo @$cutoff_month ?> or after, so use renewal_duration (<?php echo @$renewal_duration ?>) as year interval
added to current year.
<?php else:?>
It's before <?php echo @$cutoff_month ?>, so use renewal_duration -1 (<?php echo @$renewal_duration -1 ?>) as year
interval added to current year.
<?php endif ?>

The final step is to insert the $adjustedMembershipExpiryDate into the mysql_query("INSERT INTO list. NOTE: the
apostrophes are so that the variable is recognized correctly.

expiresDate = '$adjustedMembershipExpiryDate',


Then, to allow for the, “membership from date joined” option, I surrounded the calculation code and the
mysql_query("INSERT INTO code with:

<?php if ( $organization_informationRecord['valid_for_one_year_from_date_joined'] = 0 ||
$organization_informationRecord['valid_for_one_year_from_date_joined'] = '' ):?>
... date calculation code and your mysql_query("INSERT INTO code, including expiresDate =
'$adjustedMembershipExpiryDate', ...
<?php elseif (@$organization_informationRecord['valid_for_one_year_from_date_joined'] == '1'):?>
...Your original mysql_query("INSERT INTO code, including expiresDate = (NOW() + INTERVAL 1 YEAR),...
<?php endif ?>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service