*/
if (!function_exists('zen_date_raw')) {
function zen_date_raw($date)
{
$date = strtolower($date);
// get a list of abbreviated and full month names
for ($i=1; $i < 13; $i++)
{
$month_abbr[$i] = strtolower(strftime('%b',strtotime("2000-$i-1")));
$month_name[$i] = strtolower(strftime('%B',strtotime("2000-$i-1")));
}
// if DATE_FORMAT has not been defined, find it automatically by checking against the localised format
if (!defined('DATE_FORMAT'))
{
$local_format = strftime('%x', strtotime('2000-1-3'));
$local_format = str_replace(array('2000', '00', '01', '1', '03', '3'), array('Y', 'y', 'm', 'n', 'd', 'j'), $local_format);
$local_format = str_replace($month_name, 'F', $local_format);
$local_format = str_replace($month_abbr, 'M', $local_format);
define('DATE_FORMAT', $local_format);
}
// look for a full month name in the date string
foreach ($month_name as $key => $val)
{
$strpos = strpos($date, $val);
if ($strpos !== false)
{
// insert month number in front of the string where the month name was found
$date = substr_replace($date, $key, $strpos, 0);
break;
}
}
if ($strpos === false)
// a full month name was not found in the date string, now look for abbreviations
{
// find the first occurance of a month abbreviation
// this is necessary since there's a chance one abbreviation is part of another month name. thus, if the customer is entering the full month name, he would not get the correct month if we didn't do this
for ($i=1; $i < 13; $i++)
{
$month_strpos[$i] = strpos($date, $month_abbr[$i]);
if ($month_strpos[$i] !== false && (!isset($first_case) || $month_strpos[$i] < $first_case))
{
$first_case = $month_strpos[$i];
$m = $i;
}
}
if (isset($m) && isset($first_case))
{
// insert month number in front of the string where the month abbreviation was found
$date = substr_replace($date, $m, $first_case, 0);
}
}
// remove all non-numeric characters
$date = preg_replace('/[^0-9]/', '', $date);
// remove all characters except those allowed from the constant DATE_FORMAT
$dformat = preg_replace('/[^YyFMmndj]/', '', DATE_FORMAT);
// Define regex for day, month and year
$dd = '([0-2][1-9]|[1-3][0-1]|[1-9])';
if (isset($m) && isset($first_case))
{
$mm = '(' . $m . ')';
}
else
{
$mm = '(0?[1-9]|1[0-2])';
}
$yyyy = '((19|20)?[0-9]{2})';
// Look at the set date format, and create the entire regex line as well as set the location where day, month and year is to be found for later reference
if (preg_match('/(d|j)(F|M|m|n)(y|Y)/', $dformat))
{
$regexp = $dd . $mm . $yyyy;
$d = '1';
$m = '2';
$y = '3';
}
elseif (preg_match('/(F|M|m|n)(d|j)(y|Y)/', $dformat))
{
$regexp = $mm . $dd . $yyyy;
$d = '2';
$m = '1';
$y = '3';
}
elseif (preg_match('/(d|j)(y|Y)(F|M|m|n)/', $dformat))
{
$regexp = $dd . $yyyy . $mm;
$d = '1';
$m = '4';
$y = '2';
}
elseif (preg_match('/(F|M|m|n)(y|Y)(d|j)/', $dformat))
{
$regexp = $mm . $yyyy . $dd;
$d = '4';
$m = '1';
$y = '2';
}
elseif (preg_match('/(y|Y)(d|j)(F|M|m|n)/', $dformat))
{
$regexp = $yyyy . $dd . $mm;
$d = '3';
$m = '4';
$y = '1';
}
else
{
$regexp = $yyyy . $mm . $dd;
$d = '4';
$m = '3';
$y = '1';
}
// if this doesn't look like a valid date, return false
if (!preg_match('/^' . $regexp . '$/', $date, $regs))
{
return false;
}
// If some values are too short, fix them
// fix value of day
if (strlen($regs[$d]) == '1')
{
$regs[$d] = '0' . $regs[$d];
}
// fix value of month
if (strlen($regs[$m]) == '1')
{
$regs[$m] = '0' . $regs[$m];
}
// fix value of year. If year is less than or equal to the last 2 numbers of the current year, set century to 20
if (strlen($regs[$y]) === 2)
{
if ($regs[$y] <= date('y'))
{
$regs[$y] = '20' . $regs[$y];
}
else
{
$regs[$y] = '19' . $regs[$y];
}
}
// validate the date
if (!checkdate($regs[$m], $regs[$d], $regs[$y]))
{
return false;
}
// return YYYYMMDD
return $regs[$y] . $regs[$m] . $regs[$d];
} // end function zen_date_raw()
}
Bookmarks