0

In my system, I have a section for membership details. I want to be able to display a membership expiry status (eg. Current, Due to expire, and Expired) for each member. I already have expiry dates for each of them. I just need a way for the system to check what the expiry date is and display the membership status, and I have no idea how to do it.

Will be great if someone could post a detailed explanation on how to do this.

Member model:

<?php
App::uses('AppModel', 'Model');
class Member extends AppModel {
    public $displayField = 'id';

    public $belongsTo = array(
        'Contact' => array(
            'className' => 'Contact',
            'foreignKey' => 'contact_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

Member table has:
id
member_type
year_joined
date_renewed
card_issue_date
renewal_due
contact_id

Membership goes by a yearly basis. And functions just like any club membership. I need the index page and the view page to display the status of each membership by checking the "renewal_due" which is the expiry date for the membership. The status of the membership should show "Current" for membership that are still valid/not expired, "Due to Expire" for membership that is due to expiry in a month's time, and "Expired" for those that have expired.

If you need any further information, please feel free to ask. I'm not sure what kind of information is needed for this to work.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
jase89
  • 75
  • 2
  • 7
  • We need more details please, we dont know your model structure, we dont know exactly how you want to display the expiration date.. we dont know what "membership" is.. – pleasedontbelong Aug 01 '12 at 12:04
  • @pleasedontbelong Sorry about that. I've added more information. Please let me know if more is needed. Thanks – jase89 Aug 01 '12 at 12:18

1 Answers1

0

If you save your renewal date in the database as a unix timestamp in seconds you can just compare the current time to the renewal date using time()

Your controller:

<?php

public function index($memberid) {
    $this->set('member', $this->Member->find('first', array('conditions' => array('Member.id' => $memberid)));
}

?>

and in your view:

<?php
    if($member['Member']['renewal_date'] <= time()) {
        $status = 'Expired';
    } elseif (($member['Member']['renewal_date'] - (30 * 24 * 60 * 60)) <=  time()) {
        $status = 'Due To Expire';
    }  else {
        $status = 'Current';
    }
?>

Your membership is: <?php echo $status; ?>

You'll need to alter your database so that the renewal_date field is an integer and then save your renewal_date in a unix timestamp format like such:

<?php
    //sets a date one year from now
    $one_year_ahead = time() + (365 * 24 * 60 * 60);
    //alternative method
    $one_year_ahead = strtotime('+1 year');
?>
Nick Savage
  • 856
  • 1
  • 7
  • 18
  • Thanks for that. It helped. But somehow the view is displaying wrongly. The if statements dont seem to work properly. The status it displays is only "Expired" and nothing else even when I change the dates around. – jase89 Aug 01 '12 at 13:27
  • what data is getting returned? echo out the renewal date and time() and make sure that it actually isn't expired – Nick Savage Aug 01 '12 at 13:30
  • The date and time is set to 2013 but the status that is shown is "Expired". And when changing to a date a month before today, it still displays "Expired" – jase89 Aug 01 '12 at 13:37
  • Before having your solution, I was using the "Date" type for the database. Since you said to use timestamp, I changed it and now it displays this format "2013-01-17 00:00:00" – jase89 Aug 01 '12 at 13:43
  • you dont need to use timestamps. you can also compare two date strings easily. date() is your friend. using my http://www.dereuromark.de/2010/06/21/cakephp-bootstrap-goodies/ you can just compare your db date strings to `date(FORMAT_DB_DATEIME)` – mark Aug 01 '12 at 13:45
  • That is another option I just prefer using seconds, also check my edit jase – Nick Savage Aug 01 '12 at 13:52
  • This works fine but I think it's not as versatile. This is because if I were to edit the member details, it will automatically update the membership expiry date for another year. And also if a membership expires and the member wants to renew it, I wont be able to input the expiry date myself. I'd prefer to be able to enter the expiry date manually. Is that possible? Thanks for your help so far. You've been a great help. – jase89 Aug 01 '12 at 14:13
  • It just depends on how you want to enter the date. You can simply use strtotime() to change a valid date() to a unix timestamp – Nick Savage Aug 01 '12 at 14:21
  • Thanks for that Nick. I'll work on this. Hope it goes well. =) Cheers – jase89 Aug 01 '12 at 14:34