Welcome Guest, Not a member yet? Register   Sign In
Validation for date
#11

[eluser]Ochetski[/eluser]
I do suggest this option here:

https://github.com/ochetski/ci_my-form-v...dation.php


Code:
/**
  * To validate date and time
  * Date will also be tested if defined numerical values for month, year and day
  *
  * @param string $str
  * @param string $format using php format with preceded %, default %d/%m/%Y
  */
public function valid_date($str, $format = '%d/%m/%Y')
{
  $pattern = str_replace(
   array('%d', '%D', '%j', '%m', '%M', '%n', '%Y', '%y', '%G', '%g', '%H', '%h', '%i', '%s'),
   array('(?P<d>\d{2})', '(?P<D>\w{3})', '(?P<j>\d{1,2})', '(?P<m>\d{2})', '(?P<M>\w{3})', '(?P<n>\d{1,2})', '(?P<Y>\d{4})', '(?P<y>\d{2})', '(?P<G>\d{1,2})', '(?P<g>\d{1,2})', '(?P<H>\d{2})', '(?P<h>\d{2})', '(?P<i>\d{2})', '(?P<s>\d{2})'),
   addslashes($format)
  );

  if(preg_match("~{$pattern}~s", $str, $matches))
  {
   if(preg_match('~^.*?(%([dj]).*?%([mn]).*?%(Y)|%([dj]).*?%(Y).*?%([mn])|%([mn]).*?%(Y).*?%([dj])|%([mn]).*?%([dj]).*?%(Y)|%(Y).*?%([dj]).*?%([mn])|%(Y).*?%([mn]).*?%([dj])).*$~s', $format))
   {
    $d = isset($matches['d']) ? $matches['d'] : $matches['j'];
    $m = isset($matches['m']) ? $matches['m'] : $matches['n'];
    $y = $matches['Y'];

    if(checkdate($m, $d, $y))
    {
     return $str;
    }
    else
    {
     return FALSE;
    }
   }
  }
  else
  {
   return FALSE;
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB