Controller_Validator_Basic::card_date_parser PHP Method

card_date_parser() protected method

Helper for validating card to and from dates.
protected card_date_parser ( $a, $type )
    protected function card_date_parser($a, $type)
    {
        // Strip out any slash
        $date = str_replace('/', '', $a);
        // Check that we have 4 digits
        if (!preg_match('|^[0-9]{4}$|', $date)) {
            return false;
        }
        $month = substr($date, 0, 2);
        $year = substr($date, 2, 2);
        // Check month is logical
        if ($month > 12) {
            return false;
        }
        $parts = array(date('Y'), date('m'), 1);
        $now_datetime = new DateTime(implode('-', $parts));
        $parts = array('20' . $year, $month, '1');
        $card_datetime = new DateTime(implode('-', $parts));
        $interval = $now_datetime->diff($card_datetime);
        $days = $interval->format('%R%a days');
        if ($type == 'from') {
            // Check from date is older or equal to current month
            if ($days <= 0 && $days > -3650) {
                return true;
            } else {
                return false;
            }
        } elseif ($type == 'to') {
            // Check to date is newer or equal to current month
            if ($days >= 0 && $days < 3650) {
                return true;
            } else {
                return false;
            }
        } else {
            $msg = "Bad date type '{$type}' in card-date validation";
            throw new Error($msg);
        }
    }