public static function DatexFormatter::parser in Datex 7
For extracting date from formatted date strings.
File
- datex_api/
datex_api_classes.inc, line 527 - API and helper functions used by other datex modules.
Class
- DatexFormatter
- Date tools for Jalali Dates.
Code
public static function parser($date, $format) {
if (!(is_string($date) && is_string($format))) {
throw new Exception('Datex parser: date or format not valid.');
}
$granularities = array();
$j = $i = 0;
$format_len = strlen($format);
do {
while (!ctype_alpha($format[$i]) && $i < $format_len) {
$i++;
}
while (!ctype_digit($date[$j])) {
$j++;
}
switch ($format[$i]) {
case 'd':
$granularities['day'] = $date[$j] . $date[++$j];
break;
case 'j':
if (ctype_digit($date[$j + 1])) {
$granularities['day'] = $date[$j] . $date[++$j];
}
else {
$granularities['day'] = $date[$j];
}
break;
case 'm':
$granularities['month'] = $date[$j] . $date[++$j];
break;
case 'n':
if (ctype_digit($date[$j + 1])) {
$granularities['month'] = $date[$j] . $date[++$j];
}
else {
$granularities['month'] = $date[$j];
}
break;
case 'y':
case 'Y':
$granularities['year'] = $date[$j] . $date[++$j] . $date[++$j] . $date[++$j];
case 'h':
$granularities['hour'] = $date[$j] . $date[++$j];
break;
case 'g':
if (ctype_digit($date[$j + 1])) {
$granularities['hour'] = $date[$j] . $date[++$j];
}
else {
$granularities['hour'] = $date[$j];
}
break;
case 'H':
$granularities['hour'] = $date[$j] . $date[++$j];
break;
case 'G':
if (ctype_digit($date[$j + 1])) {
$granularities['hour'] = $date[$j] . $date[++$j];
}
else {
$granularities['hour'] = $date[$j];
}
break;
case 'i':
$granularities['minute'] = $date[$j] . $date[++$j];
break;
case 's':
$granularities['second'] = $date[$j] . $date[++$j];
break;
}
$j++;
} while (++$i < $format_len);
return $granularities;
}