Welcome Guest, Not a member yet? Register   Sign In
Checking for leap year
#1

[eluser]Unknown[/eluser]
I have a function within my registration controller that is a form validation callback. I need to check if it's a leap year. Currently only day is passed into the function callback. How can I access the month to check "If february is selected, and it's a leap year, check if the date is less than or equal to 29".

Here is my code to show what I mean.

I need to check the month, within the check_dob_day function. Unless there is a better solution.

Quote:<?php

class Register extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}

public function index() {
$this->load->view('register');
}

public function create() {
$this->form_validation->set_rules('firstName', 'First Name', 'trim|required|min_length[3]|max_length[25]|xss_clean|alpha');
$this->form_validation->set_rules('lastName', 'Last Name', 'trim|required|min_length[3]|max_length[25]|xss_clean|alpha');
$this->form_validation->set_rules('dayOfBirth', 'Day of Birth', 'callback_check_dob_day');
$this->form_validation->set_rules('monthOfBirth', 'Month of Birth', 'callback_check_dob_month');
$this->form_validation->set_rules('yearOfBirth', 'Year of Birth', '');

if ($this->form_validation->run() == FALSE) {
$this->load->view('register');
} else {
echo 'Success';
}
}

public function check_dob_day($day) {
// Check if leap year
if(date('L')) {

}

//
if($day <= 31) {
return TRUE;
} else {
$this->form_validation->set_message('dayOfBirth', 'D.O.B has an invalid day.');
}
}

public function check_dob_month($month) {
$validMonths = array(
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december'
);

if(!in_array($month, $validMonths)) {
$this->form_validation->set_message('monthOfBirth', 'D.O.B has an invalid month.');
return FALSE;
} else {
return TRUE;
}
}
}




Theme © iAndrew 2016 - Forum software by © MyBB