|
Odyssey
|
Public Member Functions | |
| __construct () | |
| ConvertDateFromFormat ($date, $requested_format='', $century=0) | |
Protected Member Functions | |
| SetRequestedFormat ($format) | |
| IsValidRequestedFormat ($format='') | |
| SetRequestedCentury ($century=0) | |
| FindValidFormat ($date) | |
| IsValidDate ($format, $date) | |
| ConfirmFourDigitYear ($format, $date) | |
| SplitDateOnDelimiter ($format, $date) | |
| DetectDelimiter ($format) | |
| SetFourDigitYear ($year=0) | |
| ValidateYearInput ($year=0) | |
| SetYear ($year=0) | |
| CreateRequestedDate ($current_format, $date) | |
Protected Attributes | |
| $requested_format = 'Y-m-d' | |
| $century = 0 | |
| $formats | |
Reference this class to validate and update the most used date string formats into (almost) any format compatible with PHP DateTime. This class came to life from a request to allow two digit year inputs for birthdate. Two digits . . . in what century? Think about that a second . . . combined with the IBM solution and some input options, this class proposes a solid reusable solution.
Usage: $dc = new DateConvert(); echo $dc->convertDateFromFormat([string $date], [optional string $requested_format], [optional int century);
Supports slash, dash, or dot separators in American and European formats. $requested_format will default to Y-m-d SQL format if omitted. Four digit year will be used in any case (won't be overridden.) For two digit year input, century will override the "40 year algo" used by IBM, see comments at setYear().
Definition at line 21 of file DateConvert.i.
| DateConvert::__construct | ( | ) |
|
protected |
Make sure our years are expressed in 4 digits, and hopefully in the right century. We're going to get a little messy now, like rubbing two sticks together messy. The DateTime class assumes we aren't going to do something dumb like ask it for a two digit year date. If asked, a string of 10/13/60 will return 10/13/2060. So what we have to do is deconstruct the date string old school and determine which century we need it in using the IBM algo. If that doesn't suffice, we also have the century param that will override the century calculation if needed.
| string | $format | |
| string | $date |
| Exception |
Definition at line 186 of file DateConvert.i.
| DateConvert::ConvertDateFromFormat | ( | $date, | |
$requested_format = '', |
|||
$century = 0 |
|||
| ) |
Entry point, see calling consumers/clients/apps for examples.
| string | $date | |
| string | $requested_format | |
| int | $century,overrides | 40 year rule documented at setYear() below. |
| Exception |
Definition at line 81 of file DateConvert.i.
|
protected |
Leverage the DateTime object and create a date string formatted as requested from original.
| string | $current_format | |
| string | $date |
Definition at line 340 of file DateConvert.i.
|
protected |
Extract the delimiter from the format string we found from the input date.
| $format |
Definition at line 243 of file DateConvert.i.
|
protected |
Look up from a list of date formats and see if the input date string matches on any of them. This kind of sucks, should be built dynamically but it works (and allows us control over the date formats we want to support.)
| string | $date |
| Exception |
Definition at line 142 of file DateConvert.i.
|
protected |
Core date validation, leverage the DateTime object.
| string | $format | |
| string | $date |
Definition at line 166 of file DateConvert.i.
|
protected |
If there is an optional requested output format, validate it and return a message if it's not in the cool kids' club (formats property array.)
| string | $format |
Definition at line 111 of file DateConvert.i.
|
protected |
Set a four digit year from a two digit year. If you're parsing a four digit year, you shouldn't be here, it will have already returned the date, but including the guard pattern anyway. :-)
| int | $year |
| Exception |
Definition at line 264 of file DateConvert.i.
|
protected |
Set the optional requested century to override the 40 year rule documented at setYear() below.
| int | century |
Definition at line 125 of file DateConvert.i.
|
protected |
Set the requested format into the class property.
| string | $format |
Definition at line 94 of file DateConvert.i.
|
protected |
Set a four digit year from a two digit year based on an optional provided century or year alone. From IBM:
"When you are moving or comparing 2-digit years to 4-digit years or centuries, or comparing 4-digit years or centuries to 2-digit years, ILE COBOL first converts the 2-digit year using a windowing algorithm. The default windowing algorithm used is as follows:
"If a 2-digit year is moved to a 4-digit year, the century (1st 2 digits of the year) are chosen as follows: If the 2-digit year is greater than or equal to 40, the century used is
If the 2-digit year is less than 40, the century used is 2000. In other words, 20 becomes the first 2 digits of the 4-digit year."
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzase/yrconv.htm
In our case we have added the optional param $century [int > 1000] so it can be customized by consumers (consumers being the apps calling this class.)
| int | $year |
Definition at line 317 of file DateConvert.i.
|
protected |
The rubbing two sticks together part. Split up a date string and create an array associated with keys m, d, y. Why are we doing this? To ensure a two digit year gets properly assigned as a 4 digit year. DateTime alone would always return 2060 if provided the year "60" which in most cases means 1960.
| string | $format | |
| string | $date |
Definition at line 214 of file DateConvert.i.
|
protected |
Validate year
| int | $year |
| Exception |
Definition at line 281 of file DateConvert.i.
|
protected |
Definition at line 33 of file DateConvert.i.
1.8.15