/** * Returns a valid date from a string * * This function is used in import * * @return date('Y-m-d') * */ public static function getValidDate($date) { $pattern = '/^(\\d\\d\\d\\d)(\\-(0[1-9]|1[012])(\\-((0[1-9])|1\\d|2\\d|3[01])(T(0\\d|1\\d|2[0-3])(:([0-5]\\d)(:([0-5]\\d))?))?)?)?|\\-\\-(0[1-9]|1[012])(\\-(0[1-9]|1\\d|2\\d|3[01]))?|\\-\\-\\-(0[1-9]|1\\d|2\\d|3[01])/'; if (preg_match($pattern, $date, $matches)) { $date_part = array('year' => $matches[1], 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => ''); if (isset($matches[3])) { $date_part['month'] = $matches[3]; } if (isset($matches[5])) { $date_part['day'] = $matches[5]; } if (isset($matches[8])) { $date_part['hour'] = $matches[8]; } if (isset($matches[10])) { $date_part['minute'] = $matches[10]; } if (isset($matches[12])) { $date_part['second'] = $matches[12]; } $fuzzy = new FuzzyDateTime($date_part, 56, true, $date_part['hour'] == ''); $fuzzy->setMaskFromDate($date_part); return $fuzzy; } $tmp_date = $date = str_replace('-', '/', $date); if (($dt = DateTime::createFromFormat('y', $tmp_date)) || ($dt = DateTime::createFromFormat('Y', $tmp_date))) { ($dt = DateTime::createFromFormat('y/m/d', $tmp_date . '/01/01')) || ($dt = DateTime::createFromFormat('Y/m/d', $tmp_date . '/01/01')); $tmp_date = $dt->format('Y-m-d'); } if (DateTime::createFromFormat('m/y', $tmp_date) || DateTime::createFromFormat('n/y', $tmp_date)) { $tmp_date = '01/' . $tmp_date; } if (DateTime::createFromFormat('m/Y', $tmp_date) || DateTime::createFromFormat('n/Y', $tmp_date)) { $tmp_date = '01/' . $tmp_date; } try { $fuzzy = new FuzzyDateTime($tmp_date); // try to find the mask $ctn_sep = substr_count($date, '/'); if ($ctn_sep == 0) { $fuzzy->setMask(FuzzyDateTime::getMaskFor('year')); } elseif ($ctn_sep == 1) { $fuzzy->setMask(FuzzyDateTime::getMaskFor('year') + FuzzyDateTime::getMaskFor('month')); } elseif ($ctn_sep == 2) { $fuzzy->setMask(FuzzyDateTime::getMaskFor('year') + FuzzyDateTime::getMaskFor('month') + FuzzyDateTime::getMaskFor('day')); } elseif ($ctn_sep == 3) { $fuzzy->setMask(FuzzyDateTime::getMaskFor('year') + FuzzyDateTime::getMaskFor('month') + FuzzyDateTime::getMaskFor('day') + FuzzyDateTime::getMaskFor('hour')); } elseif ($ctn_sep == 4) { $fuzzy->setMask(FuzzyDateTime::getMaskFor('year') + FuzzyDateTime::getMaskFor('month') + FuzzyDateTime::getMaskFor('day') + FuzzyDateTime::getMaskFor('hour') + FuzzyDateTime::getMaskFor('minute')); } elseif ($ctn_sep == 5) { $fuzzy->setMask(FuzzyDateTime::getMaskFor('year') + FuzzyDateTime::getMaskFor('month') + FuzzyDateTime::getMaskFor('day') + FuzzyDateTime::getMaskFor('hour') + FuzzyDateTime::getMaskFor('minute') + FuzzyDateTime::getMaskFor('second')); } return $fuzzy; } catch (Exception $e) { } return null; }