PHP-Code:
<?php
error_reporting(-1);
$inbound = isset($_POST['evalToDate']) ? trim($_POST['evalToDate']) : false;
if ( $inbound ) {
$possibleDateSchemes = array(
'Y',
'd',
'dm',
'd.m',
'd.m.',
'd.m.Y',
'd.m.y'
);
$earnedDates = array();
$failedSchemes = array();
foreach ( $possibleDateSchemes as $dateScheme )
{
$cur = date_create_from_format($dateScheme, $inbound, new DateTimeZone('GMT'));
if ( $cur instanceof DateTime ) {
$curErrors = $cur->getLastErrors();
$curErrors = array_filter($curErrors, 'is_numeric');
$curErrors = (bool) array_sum($curErrors);
if ( $curErrors ) {
$failedSchemes[ $inbound ][] = $dateScheme;
continue;
}
$earnedDates[ $dateScheme ] = clone $cur;
}
else {
$failedSchemes[ $inbound ][] = $dateScheme;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Date Test</title>
</head>
<body>
<form target="" method="post">
<input type="text" name="evalToDate" value="" />
</form>
<div>
<pre><?php if ( isset($earnedDates) ) {
if ( count($earnedDates) > 0 ) {
var_export($earnedDates);
}
else {
echo 'invalid input';
}
} ?>
<hr />
<?php if ( isset($failedSchemes) ) {
if ( count($failedSchemes) > 0 ) {
var_export($failedSchemes);
}
else {
echo 'invalid input';
}
} ?></pre></div>
</body>
</html>
Kommentar