1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
private function parse_date($str){ $pattern = '/^([12]\d{3})[\/-]{1}(\d{1,2})[\/-]{1}(\d{1,2})$/'; preg_match($pattern, $str, $matchs); if($matchs == false){ $this->show_error($str . ' is not a valid date'); } //Check year $now = Carbon::now(); $year = $matchs[1]; if($year < ($now->year - 3) || $year > ($now->year + 3)){ $this->show_error('Year should be between ' . ($now->year - 3) . ' and ' . ($now->year + 3)); } //Check month $month = $matchs[2]; if($month < 1 || $month > 12){ $this->show_error($str . ' is not a valid date'); } //Check max day $m = Carbon::create($year, $month); $day = $matchs[3]; if($day < 1 || $day > $m->daysInMonth){ $this->show_error($str . ' is not a valid date'); } $date = Carbon::create($year, $month, $day); return $date->toDateString(); } |