public static function parseDate( $value, $pattern = self::DEFAULT_DATE_FORMAT )
{
$tokens = self::tokenize($pattern);
$i = 0;
$n = strlen($value);
foreach ( $tokens as $token )
{
switch ( $token )
{
case 'yyyy':
{
if ( ($year = self::parseInteger($value, $i, 4, 4)) === false )
return null;
$i+=4;
break;
}
case 'yy':
{
if ( ($year = self::parseInteger($value, $i, 1, 2)) === false )
return null;
$i+=strlen($year);
break;
}
case 'MM':
{
if ( ($month = self::parseInteger($value, $i, 2, 2)) === false )
return null;
$i+=2;
break;
}
case 'M':
{
if ( ($month = self::parseInteger($value, $i, 1, 2)) === false )
return null;
$i+=strlen($month);
break;
}
case 'dd':
{
if ( ($day = self::parseInteger($value, $i, 2, 2)) === false )
return null;
$i+=2;
break;
}
case 'd':
{
if ( ($day = self::parseInteger($value, $i, 1, 2)) === false )
return null;
$i+=strlen($day);
break;
}
case 'h':
case 'H':
{
if ( ($hour = self::parseInteger($value, $i, 1, 2)) === false )
return null;
$i+=strlen($hour);
break;
}
case 'hh':
case 'HH':
{
if ( ($hour = self::parseInteger($value, $i, 2, 2)) === false )
return null;
$i+=2;
break;
}
case 'm':
{
if ( ($minute = self::parseInteger($value, $i, 1, 2)) === false )
return null;
$i+=strlen($minute);
break;
}
case 'mm':
{
if ( ($minute = self::parseInteger($value, $i, 2, 2)) === false )
return null;
$i+=2;
break;
}
case 's':
{
if ( ($second = self::parseInteger($value, $i, 1, 2)) === false )
return null;
$i+=strlen($second);
break;
}
case 'ss':
{
if ( ($second = self::parseInteger($value, $i, 2, 2)) === false )
return null;
$i+=2;
break;
}
default:
{
$tn = strlen($token);
if ( $i >= $n || substr($value, $i, $tn) !== $token )
return null;
$i+=$tn;
break;
}
}
}
if ( $i < $n )
{
return false;
}
if ( !isset($year) || !isset($month) || !isset($day) )
{
return null;
}
if ( strlen($year) === 2 )
{
if ( $year > 70 )
$year+=1900;
else
$year+=2000;
}
$year = (int) $year;
$month = (int) $month;
$day = (int) $day;
if ( !isset($hour) && !isset($minute) && !isset($second) )
{
$hour = $minute = $second = 0;
}
else
{
if ( !isset($hour) )
{
$hour = 0;
}
if ( !isset($minute) )
{
$minute = 0;
}
if ( !isset($second) )
{
$second = 0;
}
$hour = (int) $hour;
$minute = (int) $minute;
$second = (int) $second;
}
return array(
'minute' => $minute,
'hour' => $hour,
'second' => $second,
'day' => $day,
'month' => $month,
'year' => $year);
// $dateTime = new DateTime();
// $dateTime->setDate( $year, $month, $day );
// $dateTime->setTime( $hour, $minute, $second );
//
// return $dateTime;
}
private static function tokenize( $pattern )
{
if ( !($n = strlen($pattern)) )
return array();
$tokens = array();
for ( $c0 = $pattern[0], $start = 0, $i = 1; $i < $n; ++$i )
{
if ( ($c = $pattern[$i]) !== $c0 )
{
$tokens[] = substr($pattern, $start, $i - $start);
$c0 = $c;
$start = $i;
}
}
$tokens[] = substr($pattern, $start, $n - $start);
return $tokens;
}
protected static function parseInteger( $value, $offset, $minLength, $maxLength )
{
for ( $len = $maxLength; $len >= $minLength; --$len )
{
$v = substr($value, $offset, $len);
if ( ctype_digit($v) )
return $v;
}
return false;
}