views:

358

answers:

4

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).

+6  A: 

As a unix timestamp is a integer, use is_int(). However as is_int() doesn't work on strings, we check if it is numeric and its intergal form is the same as its orignal form. Example:

( is_numeric($stamp) && (int)$stamp == $stamp )
Yacoby
To express timestamps with milliseconds, the unixtimestamp may also be a floating point in some areas, right?
Patrick Cornelissen
@Patrick A Unix time stamp is usually an integer due to floating point precision. I have never come across it represented as a real number.
Yacoby
The OP has a string, so is_int won't do. Has to be is_numeric if any.
Gordon
@Gordon Yay for the inconstancies of PHP. Fixed.
Yacoby
+1  A: 

You want to check if a string contains a high number?

is_numeric() is the key

Or convert it to DateTime and do some checks with it like an expected date range.

Patrick Cornelissen
+7  A: 

Ok, after fiddling with this for some time, I withdraw the solution with date('U') and suggest to use this one instead:

function isValidTimeStamp($timestamp)
{
    return ((string) (int) $timestamp === $timestamp) 
        && ($timestamp <= PHP_INT_MAX)
        && ($timestamp >= ~PHP_INT_MAX);
}

This check will only return true if the given $timestamp is 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).

var_dump( isValidTimeStamp(1)             ); // false
var_dump( isValidTimeStamp('1')           ); // TRUE
var_dump( isValidTimeStamp('1.0')         ); // false
var_dump( isValidTimeStamp('1.1')         ); // false
var_dump( isValidTimeStamp('0xFF')        ); // false
var_dump( isValidTimeStamp('0123')        ); // false
var_dump( isValidTimeStamp('01090')       ); // false
var_dump( isValidTimeStamp('-1000000')    ); // TRUE
var_dump( isValidTimeStamp('+1000000')    ); // false
var_dump( isValidTimeStamp('2147483648')  ); // false
var_dump( isValidTimeStamp('-2147483649') ); // false

The check for PHP_INT_MAX is to ensure that your string can be used correctly by date and the likes, e.g. it ensures this doesn't happen*:

echo date('Y-m-d', '2147483648');  // 1901-12-13
echo date('Y-m-d', '-2147483649'); // 2038-01-19

(*) Note: I'm not 100% sure, the bit range corresponds with what date can use though

Gordon
shouldn't this be === ?
Patrick Cornelissen
+1 Nice idea of using the PHP libraries to do the validation.
Yacoby
@Patrick I don't think so. `date` returns a string.
Yacoby
ah, you are right...
Patrick Cornelissen
I *think* negative timestamps are valid. `time_t` is signed in Linux at least.
Yacoby
@Yacoby removed that part. is_integer wouldn't work for the input *strings* the OP asked for anyway.
Gordon
It didn't work for a value of 1276664400, probably due to the value being passed back as a string as opposed to a integer.
jtyost2
@jtyost It works as explained and demanded by the OP. It checks whether the **string** is a valid Unix Timestamp. Passing in an **integer** will obviously not return true. Passing in `"1276664400"` will. So please remove the downvote now.
Gordon
A: 

This doesn't account for negative times(before 1970), nor does it account for extended ranges(you can use 64 bit integers so that a timestamp can represent a value far after 2038)

$valid = ctype_digit($str) && $str <= 2147483647;
chris