24 protected $requested_format =
'Y-m-d';
30 protected $century = 0;
33 protected $formats = [
35 'm/d/Y',
'n/d/Y',
'n/j/Y',
'm/j/Y',
37 'm/d/y',
'n/d/y',
'n/j/y',
'm/j/y',
39 'm.d.Y',
'n.d.Y',
'n.j.Y',
'm.j.Y',
41 'm.d.y',
'n.d.y',
'n.j.y',
'm.j.y',
43 'm-d-Y',
'n-d-Y',
'n-j-Y',
'm-j-Y',
45 'm-d-y',
'n-d-y',
'n-j-y',
'm-j-y',
47 'Y/m/d',
'Y/n/d',
'Y/n/j',
'Y/m/j',
49 'y/m/d',
'y/n/d',
'y/n/j',
'y/m/j',
51 'Y.m.d',
'Y.n.d',
'Y.n.j',
'Y.m.j',
53 'y.m.d',
'y.n.d',
'y.n.j',
'y.m.j',
55 'm.d.Y',
'n.d.Y',
'm.j.Y',
'n.j.Y',
57 'm.d.y',
'n.d.y',
'm.j.y',
'n.j.y',
59 'Y-m-d',
'Y-n-d',
'Y-n-j',
'Y-m-j',
61 'y-m-d',
'y-n-d',
'y-n-j',
'y-m-j',
84 ->SetRequestedFormat($requested_format)
85 ->SetRequestedCentury($century)
86 ->FindValidFormat($date);
100 $this->requested_format = $format;
113 if (empty($format) || ! in_array($format, $this->formats)) {
127 if (intval($century) > 0) {
128 $this->century = intval($century);
144 foreach ($this->formats as $fmt) {
151 $fmt = preg_replace(
'/y/',
'Y', $fmt);
168 $obj = DateTime::createFromFormat($format, $date);
169 return $obj && $obj->format($format) == $date;
190 if (strlen($date_arr[
'y']) >= 4) {
196 $format = preg_replace(
'/y/',
'Y', $format);
199 $obj->setDate($y, $date_arr[
'm'], $date_arr[
'd']);
201 return $obj->format($format);
219 $keys = explode($delim, $format);
220 $vals = explode($delim, $date);
222 for ($i = 0; $i < count($keys); $i++) {
224 if (in_array(strtolower($keys[$i]), [
'm',
'n'])) {
225 $arr[
'm'] = $vals[$i];
227 if (in_array(strtolower($keys[$i]), [
'd',
'j'])) {
228 $arr[
'd'] = $vals[$i];
230 if (strtolower($keys[$i]) ==
'y') {
231 $arr[
'y'] = $vals[$i];
245 $supported = [
'/',
'.',
'-'];
247 foreach ($supported as $delim) {
248 if (preg_match(
"|$delim|", $format)) {
268 if (strlen($year) >= 4) {
272 return $this->
SetYear(intval($year));
283 if (intval($year) < 0) {
284 throw new Exception(
"Please enter a numeric year." . PHP_EOL);
287 if (intval($year) > 99) {
288 throw new Exception(
"Please enter a two digit year." . PHP_EOL);
320 $year = intval($year);
321 $century = intval($this->century);
324 $century = ($year >= 40)? 1900 : 2000;
329 $century = intdiv($century, 100) * 100;
331 return $year += $century;
342 $obj = DateTime::createFromFormat($current_format, $date);
343 return $obj->format($this->requested_format);
ValidateYearInput($year=0)
SetFourDigitYear($year=0)
SetRequestedCentury($century=0)
ConfirmFourDigitYear($format, $date)
SplitDateOnDelimiter($format, $date)
IsValidDate($format, $date)
SetRequestedFormat($format)
ConvertDateFromFormat($date, $requested_format='', $century=0)
CreateRequestedDate($current_format, $date)
IsValidRequestedFormat($format='')