Created
February 1, 2016 18:31
-
-
Save marcushat/f4e88d61d61af6bc957d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function range_parse($date, $default_year) { | |
$get_month_num = function($month_name) { | |
$month_num = false; //default | |
for($i=1; $i <= 12; $i++) { | |
$month_start = mktime(12,0, 0, $i, 1); | |
$month_long = date('F', $month_start); | |
$month_short = date('M', $month_start); | |
$month_long = mb_strtolower($month_long); | |
$month_short = mb_strtolower($month_short); | |
if( | |
mb_stripos($month_name, $month_long) === 0 || | |
mb_stripos($month_name, $month_short) === 0 | |
) { | |
//pop off month then try to parse year and day | |
$month_num = $i; | |
break; | |
} | |
} | |
return $month_num; | |
}; | |
$to_full_year = function($year, $split_point = 65) { | |
if( | |
$year < 100 && | |
$year >= $split_point | |
) { | |
$year = "19$year"; | |
} elseif( | |
$year >= 0 && | |
$year < $split_point | |
) { | |
$year = "20$year"; | |
} | |
return $year; | |
}; | |
$strip_empty = function(array $array){ | |
$stripped = array_filter($array, function($el) { | |
return ($el == '') ? false : true; | |
}); | |
$stripped = array_values($stripped); | |
return $stripped; | |
}; | |
$date = trim($date); | |
$date = mb_strtolower($date); | |
$range = new stdClass; | |
$month_names = []; | |
for($i=1; $i <= 12; $i++) { | |
$month_start = mktime(12,0, 0, $i, 1); | |
$month_long = date('F', $month_start); | |
$month_short = date('M', $month_start); | |
$month_names[] = mb_strtolower($month_long); | |
$month_names[] = mb_strtolower($month_short); | |
} | |
$months_or = implode('|', $month_names); | |
if( //month | |
preg_match("/^($months_or)\s*(,|\s)\s*0*((19|20)[0-9]{2})$/", $date) || // jan ,yyyy | |
preg_match("/^($months_or)\s*,\s*0*([0-9]{2})$/", $date) || // jan ,yy | |
preg_match("/^($months_or)$/", $date) // jan | |
) { | |
$date = preg_replace('/(,|\s)/', ' ', $date); | |
$date = explode(' ', $date); | |
$date= $strip_empty($date); | |
$date = array_pad($date, 2, ''); | |
list($month, $year) = $date; | |
$month = ctype_digit($month) ? $month : $get_month_num($month); | |
$year = ($year == '') ? $default_year : $year; | |
$year = $to_full_year($year); | |
$start = mktime(0, 0, 0, $month, 1, $year); | |
$days_in_month = date('t', $start); | |
$month_length = $days_in_month * 24 * 60 * 60; | |
$range->type = 'month'; | |
$range->formatted = date('F, Y', $start); | |
$range->start = $start; | |
$range->length = $month_length; | |
$range->end = $range->start + $range->length; | |
} elseif( //day | |
preg_match("/^0*(1[0-2]|[1-9])\/0*(3[0-1]|[1-2][0-9]|[1-9])\/0*((19|20)?[0-9]{2})$/", $date) || // m/d/y | |
preg_match("/^($months_or)\s0*(3[0-1]|[1-2][0-9]|[1-9])\s*(,|\s)\s*0*((19|20)?[0-9]{2})$/", $date) || // jan 03, y | |
preg_match("/^0*(1[0-2]|[1-9])\/0*(3[0-1]|[1-2][0-9]|[1-9])$/", $date) || // mm/dd | |
preg_match("/^($months_or)\s*0*(3[0-1]|[1-2][0-9]|[1-9])$/", $date) // jan dd | |
) { | |
$date = preg_replace('/(,|\s|\/)/', ' ', $date); | |
$date = explode(' ', $date); | |
$date = $strip_empty($date); | |
list($month, $day, $year) = array_pad($date, 3, ''); | |
$month = ctype_digit($month) ? $month : $get_month_num($month); | |
$year = ($year == '') ? $default_year : $year; | |
$year = $to_full_year($year); | |
// print_r_pre($year); exit; | |
$valid = checkdate($month, $day, $year); | |
if($valid) { | |
$start = mktime(0, 0, 0, $month, $day, $year); | |
$range->type = 'day'; | |
$range->formatted = date('F j, Y', $start); | |
$range->start = $start; | |
$range->length = 24 * 60 * 60; | |
$range->end = $range->start + $range->length; | |
} else { | |
$range = false; | |
} | |
} elseif( //year | |
preg_match("/^((19|20)?[0-9]{2})$/", $date) // y | |
) { | |
$year = date($date); | |
$year = $to_full_year($year); | |
$start = mktime(0, 0, 0, 1, 1, $year); | |
$is_leap_year = date('L', $start); | |
$days_in_year = $is_leap_year ? 366 : 365; | |
$year_length = $days_in_year * 24 * 60 * 60; | |
$range->type = 'year'; | |
$range->formatted = date('Y', $start); | |
$range->start = $start; | |
$range->length = $year_length; | |
$range->end = $range->start + $range->length; | |
} else { | |
$range = false; | |
} | |
return $range; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Accepted formats:
Day Formats:
March d, yyyy
March d,yyyy
March d yyyy
March d yy
March d,yy
March d,0000yy
March d,0000yyyy(leading zeros)
m/d/y
m/d
March d
Month Formats;
March
Mar
Mar yy
Mar yyyy
Year Formats:
yyyy
yy(leading zeros not supported here)
Case-insensitive. Full and 3 letter month formats supported. Leading zeros supported except with "yy" year format