$t->is(FuzzyDateTime::getDateTimeStringFromArray($testArray, $fdt->getStart(), $fdt->getWithTime()), $testDateTime2, 'Date without hours - start flag set at false: Extraction of date from array succeeded !'); $testArray['hour'] = '13'; $testDateTime2 = '1975/02/24 13:00:00'; $t->is(FuzzyDateTime::getDateTimeStringFromArray($testArray, $fdt->getStart(), $fdt->getWithTime()), $testDateTime2, 'Date without minutes - start flag set at false: Extraction of date from array succeeded !'); $testArray['minute'] = '12'; $testDateTime2 = '1975/02/24 13:12:00'; $t->is(FuzzyDateTime::getDateTimeStringFromArray($testArray, $fdt->getStart(), $fdt->getWithTime()), $testDateTime2, 'Date without seconds - start flag set at false: Extraction of date from array succeeded !'); $testArray['second'] = '11'; $testDateTime2 = '1975/02/24 13:12:11'; $t->is(FuzzyDateTime::getDateTimeStringFromArray($testArray, $fdt->getStart(), $fdt->getWithTime()), $testDateTime2, 'Date with everything - start flag set at false: Extraction of date from array succeeded !'); $testArray['day'] = ''; $testDateTime2 = '1975/02/01'; $t->is(FuzzyDateTime::getDateTimeStringFromArray($testArray, $fdt->getStart()), $testDateTime2, 'Date without hours - start flag set at false: Extraction of date from array succeeded !'); $testArray['day'] = '24'; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 63, 'The Mask returned by getMaskFromDate is correct !'); $fdt->setMaskFromDate($testArray); $t->is($fdt->getMask(), 63, 'Setting of mask from date array worked !'); $testArray['second'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 62, 'The Mask returned by getMaskFromDate is correct !'); $testArray['minute'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 60, 'The Mask returned by getMaskFromDate is correct !'); $testArray['hour'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 56, 'The Mask returned by getMaskFromDate is correct !'); $testArray['day'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 48, 'The Mask returned by getMaskFromDate is correct !'); $testArray['month'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 32, 'The Mask returned by getMaskFromDate is correct !'); $testArray['year'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 0, 'The Mask returned by getMaskFromDate is correct !'); $fdt->setDateFormat('d/m/Y'); $fdt->setTimeFormat('H:i:s');
/** * 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; }