I am passing a list of values to a function to convert to and from specific type of values. For example, if a PHP Date is passed, or a timestamp is passed, I want to convert to a specific date format. Here is the code I have now:
public static function attemptParseString($str) {
$check = strtotime($str);
if ($check !== FALSE) {
return date(ts_default_date_format, $check).' <span class="grey">(Orig: '.$str.')</span>';
}
$check = date(ts_default_date_format, $str);
if ($check !== FALSE) {
return $check.' <span class="grey">(Orig: '.$str.')</span>';
}
return $str;
}
Most date values (like '1361215542' and '2013-02-18 13:25:42') return as expected, but I get some weird issues with things like '127.0.0.1' parsing as '1969-12-31 18:02:07', 'GET' parsing as '2013-02-18 04:25:42' and '80' parsing as '1969-12-31 18:01:20'.
How can I intelligently decide whether a string is a valid date, besides checking if the date function parses it as a date?
Ive seen similar questions, like the following, but they weren't quite right (some were a different programming language, and some contained all valid dates but in different formats [whereas I contain dates and non date strings).
Problem getting date with Universal Feed Parser Date parser for all formats Unexpected result from Date() function
Thanks!