Codeigniter 如何在用户的注册日期中添加一年
Posted
技术标签:
【中文标题】Codeigniter 如何在用户的注册日期中添加一年【英文标题】:Codeigniter how to add a year to user's signup date 【发布时间】:2011-05-27 23:51:14 【问题描述】:我有一个用户注册表单,我需要插入一个函数来创建客户的到期日期。 1 年或 2 年,具体取决于下拉选项。
如何在创建用户时将年份值添加到注册日期?
这是我的新用户控制器
function new_member()
$data = array(
'Last_Name' => $this->input->post('Last_Name'),
'First_Name' => $this->input->post('First_Name'),
'Salutation' => $this->input->post('Salutation'),
'Address' => $this->input->post('Address'),
'City' => $this->input->post('City'),
'Province' => $this->input->post('Province'),
'Postal' => $this->input->post('Postal'),
'Email' => $this->input->post('Email'),
'Dial_Up' => $this->input->post('Dial_Up'),
'Phone_1' => $this->input->post('Phone_1'),
'Phone_2' => $this->input->post('Phone_2'),
'Language' => $this->input->post('Language'),
'Membership_Cat' => $this->input->post('Membership_Cat'),
'Special_Member_Cat' => $this->input->post('Special_Member_Cat'),
'Membership_Status' => $this->input->post('Membership_Status'),
'Date_DNR' => $this->input->post('Date_DNR'),
'Gift_Membership' => $this->input->post('Gift_Membership'),
'Gift_Giver' => $this->input->post('Gift_Giver'),
'Membership_Length' => $this->input->post('Membership_Length'),
'Dues_Paid' => $this->input->post('Dues_Paid'),
'Donation' => $this->input->post('Donation'),
'Payment_Method' => $this->input->post('Payment_Method'),
'Date_Received' => $this->input->post('Date_Received'),
'Date_Input' => $this->input->post('Date_Input'),
'Membership_Date' => $this->input->post('Membership_Date'),
'Renew_Letter_1' => $this->input->post('Renew_Letter_1'),
'Renew_Letter_2' => $this->input->post('Renew_Letter_2'),
'Renew_Letter_3' => $this->input->post('Renew_Letter_3'),
'Date_Letter_Sent' => $this->input->post('Date_Letter_Sent'),
'Vol_FA' => $this->input->post('Vol_FA'),
'Vol_TEL' => $this->input->post('Vol_TEL'),
'Vol_CD' => $this->input->post('Vol_CD'),
'Vol_SCBA' => $this->input->post('Vol_SCBA'),
'Vol_MAIL' => $this->input->post('Vol_MAIL'),
'Vol_SML' => $this->input->post('Vol_SML'),
'Vol_BODCOM' => $this->input->post('Vol_BODCOM'),
'Vol_OTHER' => $this->input->post('Vol_OTHER'),
'Member_Since' => $this->input->post('Member_Since'),
'Notes' => $this->input->post('Notes'),
);
$this->membership_model->add_record($data);
$this->load->view('index_view');
这是我的模特
function add_record($data)
$this->db->insert('Membership', $data);
return;
感谢任何帮助!
谢谢
【问题讨论】:
【参考方案1】:我接受了您的建议并研究了 strtotime 函数。由于我有 2 年的会员资格,即 1 年和 2 年,因此我还需要在函数中提出一个变量。刚测试完,就到这里了
$Membership_Length = $this->input->post('Membership_Length');
if($Membership_Length == "1")
$length = "+1 year";
else
$length = "+2 years";
$Expiry_Date1 = $this->input->post('Membership_Date');
$Expiry_Date = strtotime($length , strtotime($Expiry_Date1));
$Expiry_Date = date('Y-m-j', $Expiry_Date);
$date_expiry_array = array('Expiry_Date' => $Expiry_Date);
$data = array_merge($data, $date_expiry_array);
【讨论】:
【参考方案2】:通过您设置数据数组的方式,您可以使用如下选项:
$date_received = $this->input->post('Date_Received');
$date_expiry = strtotime(date("Y-m-d", strtotime($date_received)). " +1 year";
$date_expiry_array = array('Date_Received'=>$date_expiry);
$data = array_merge($data, $date_expiry_array);
或者如果你使用了这种方法:
$date_received = $this->input->post('date_received');
$data['date_received'] = strtotime(date("Y-m-d", strtotime($date_received)). " +1 year
";
如果您想测试 1 年或 2 年的到期长度,您可以添加一个 if 语句来测试 date_received 的值,然后根据该值添加年数。
【讨论】:
【参考方案3】:查看 php strtotime() 函数,例如
$expiry = date('yy-mm-dd', strtotime('+1 year'));
【讨论】:
以上是关于Codeigniter 如何在用户的注册日期中添加一年的主要内容,如果未能解决你的问题,请参考以下文章
如何在codeigniter中使用两个不同的表只显示一次数据