I have a string and I need to find out whether it is a unix timestamp or not, how can I do that effectively?
I found this thread via Google, but it doesn't come up with a very solid answer, I'm afraid. (And yes, I cribbed the question from the original poster on the aforementioned thread).
Ok, after fiddling with this for some time, I withdraw the solution with
date('U')and suggest to use this one instead:
This check will only return true if the given
$timestampis a string and consists solely of digits and an optional minus character. The number also has to be within the bit range of an integer (EDIT: actually unneeded as shown here).
The check for PHP_INT_MAX is to ensure that your string can be used correctly by
dateand the likes, e.g. it ensures this doesn't happen*:
On 64bit systems the integer is of course larger than that and the function will no longer return false for "2147483648" and "-2147483649" but for the corresponding larger numbers.
(*) Note: I'm not 100% sure, the bit range corresponds with what date can use though