public function setGtuToDate($fd) { if ($fd instanceof FuzzyDateTime) { $this->_set('gtu_to_date', $fd->format('Y/m/d H:i:s')); $this->_set('gtu_to_date_mask', $fd->getMask()); } else { $dateTime = new FuzzyDateTime($fd, 56, false, true); if (is_array($fd)) { $dateTime->setMask(FuzzyDateTime::getMaskFromDate($fd)); } $this->_set('gtu_to_date', $dateTime->format('Y/m/d H:i:s')); $this->_set('gtu_to_date_mask', $dateTime->getMask()); } }
$t->is(FuzzyDateTime::checkDateTimeStructure($testArray), 'month_missing', 'Date structure test ok'); $testArray['year'] = ''; $t->is(FuzzyDateTime::checkDateTimeStructure($testArray), 'year_missing', 'Date structure test ok'); $testArray['year'] = '1975'; $testArray['month'] = '02'; $testArray['day'] = ''; $testArray['hour'] = '02'; $t->is(FuzzyDateTime::checkDateTimeStructure($testArray), 'day_missing', 'Date structure test ok'); $testArray['year'] = '0001'; $testArray['month'] = '01'; $testArray['day'] = '01'; $fdt = new FuzzyDateTime($testArray); $t->is($fdt->format('d/m/Y'), '01/01/0001', 'Date lower bound format ok'); $testArray = array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => ''); $fdt = new FuzzyDateTime('2009/12/05'); $t->is_deeply($fdt->getDateTimeMaskedAsArray(), $testArray, 'array is the same'); $testArray = array('year' => 2009, 'month' => 12, 'day' => '', 'hour' => '', 'minute' => '', 'second' => ''); $fdt->setMask(48); $t->is_deeply($fdt->getDateTimeMaskedAsArray(), $testArray, 'array is the same'); $testArray = array('year' => 2009, 'month' => 12, 'day' => 05, 'hour' => '', 'minute' => '', 'second' => ''); $fdt->setMask(56); $t->is_deeply($fdt->getDateTimeMaskedAsArray(), $testArray, 'array is the same'); $testArray = array('year' => 2009, 'month' => 2); $t->is('2009/02/28', FuzzyDateTime::getDateTimeStringFromArray($testArray, false), 'Date are nicelly completed with missing day in fev'); $testArray = array('year' => 2009, 'month' => 12); $t->is('2009/12/31', FuzzyDateTime::getDateTimeStringFromArray($testArray, false), 'Date are nicelly completed with missing day in decembre'); $testArray = array('year' => 2009, 'month' => 100); $t->is('2038/12/31', FuzzyDateTime::getDateTimeStringFromArray($testArray, false), 'Default date is set from wrong dates'); $fdt = new FuzzyDateTime(array('year' => '2005', 'month' => '02', 'day' => '02', 'hour' => '01', 'minute' => 0, 'second' => 0), 63, false, true); $fdt->setDateFormat('Y/m/d'); $t->is($fdt->getDateMasked(), '2005/02/02 01:00:00', 'Default date is set from wrong dates');
/** * 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; }