I need a function to validate if the content of a variable is a valid unix timestamp.
I've checked this thread: Check whether the string is a unix timestamp but the problem is that the function in the most voted answer still can cause some errors, for eg:
function is_timestamp($timestamp)
{
return ((string) (int) $timestamp === $timestamp)
&& ($timestamp <= PHP_INT_MAX)
&& ($timestamp >= ~PHP_INT_MAX);
}
// Some tests:
$timestamp = 1289216307;
var_dump(is_timestamp($timestamp));
var_dump(date('Y-m-d', $timestamp));
// result
bool(false)
string(10) "2010-11-08"
$timestamp = '20101108';
var_dump(is_timestamp($timestamp));
var_dump(date('Y-m-d', $timestamp));
// result
bool(true)
string(10) "1970-08-21"
Here, the first should be TRUE and the second should be FALSE so, what's the best way to test if a $var
is a real valid unix timestamp?