Check whether the string is timestamp

Hello,

I have a string and I need to find out whether it is a unix timestamp or not, how can I do that effectively?


function isTimestamp( $string ) {
    return ( 1 === preg_match( '~^[1-9][0-9]*$~', $string ) );
}

Since time stamp is a numeric value, so you can simply check with is_numeric() function. 123 is also the correct time stamp. Or if you can type cast with (int)$string as well and then check as above said.

Ok thanks for that. One more question, what if I have integer variable and want to check whether it is timestamp?

   		Since time stamp is a numeric value, so you can simply check with [is_numeric()](http://www.php.net/manual/en/function.is-numeric.php) function. 123 is also the correct time stamp. Or if you can type cast with (int)$string as well and then check as above said.

Actually is_numeric will return true for things like ‘12e9’ or ‘12.3’ and is not a very good choice. Casting directly to integer is not good either because a string like ‘test’ will return 0 (which is a valid timestamp). See my revised answer which I think is better.

‘test’ isn’t a valid unix timestamp, why would it be valid? It has no meaning as timestamp.

Ok thanks for that. One more question, what if I have integer variable and want to check whether it is timestamp?

Just check that is a positive integer (greather than or equal to 0).

	 		'test' isn't a valid unix timestamp, why would it be valid? It has no meaning as timestamp. 	

I was talking about casting the string ‘test’ to an integer which would yield the value 0.

Oh yes… that was not really my experiment but it was a quick thought. Yes you are right that is_numeric() will not work. But this ctype_digit() will work well as far as i know.

Thanks.

Actually I need to differentiate between normal integers (like 1, 2, 257) and unix timestamps so I did this:


if (is_int($variable))
{
    if (strlen($variable) === 10) // it's a timestamp
        // do something
    else // it's normal integer
        // do something
}

Unix timestamps aren’t always 10 digits long, remember.

Indeed. Even 2000 can also be correct unix time stamp and you can get date:


echo date('Y-m-d H:i:s', 2300); # here 2300 is the unix time stamp.

here 2300 is the unix time stamp and will give you the output:


1970-01-01 00:38:20

So starting from 0 unix time stamp is epoch from 1970.

Depends on what type of context you are using it in. What is considered a valid time index? 1 year ago? 5 years ago? 20 years ago?

if ( ($variable >= (time() - 20 * 365 * 24 * 60 * 60)) && ($variable <= time()) ) {
	## This is a valid timestamp
}

This will work for time ranges between now and 20 years ago, adjust accordingly.

No the unix timestamp is the output of time() function - so it is always 10 digits long.

They are 10 digits right now.

What about September 6, 2014 at around 11:50:08PM? How many digits will a unix timestamp be then?

Check that, think its a 32-bit error.

10000000000 would be 11/20/2286 12:46:40 I think.

Still timestamps can be less than 10 digits, depending on how far back you want to go.

Ok then… but that means there is no method how to tell the normal integer from unix timestamp…

There’s really nothing special that discerns a timestamp from a normal positive integer.

The best thing to do is to just pick a range and then use a check to see if the integer falls within that range.

What range should I pick?

EDIT: maybe this would be good enough:

if (strlen($variable) >= 10)

I won’t work with very high integers.

Instead of using Unix timestamp why not use the better alternative ISO 8601?

I even wrote a pattern to work with a strict (year is required, and hyphens and colons must be used) form of ISO 8601


/^
(?:
    (?P<year>
        [-+]\\d{4,}
        |
        \\d{4}
    )
    (?:
        (?:-
            (?P<month>
                1[012]
                |
                0[1-9]
            )
            (?:-
                (?P<day>
                    3[01]
                    |
                    [12]\\d
                    |
                    0[1-9]
                )
            )?
        )
        |
        (?:-[Ww]
            (?P<yearweek>
                5[0-3]
                |
                [1-4]\\d
                |
                0[1-9]
            )
            (?:-
                (?P<weekday>[1-7])
            )?
        )
        |
        (?:-
            (?P<yeardays>
                36[0-6]
                |
                3[0-5]\\d
                |
                [12]\\d{2}
                |
                0[1-9]\\d
                |
                00[1-9]
            )
        )
    )?
)
(?:
    (?:[Tt]| +)
    (?P<hour>
        2[0-4]
        |
        [01]\\d
    )
    (?:\\:
        (?P<minutes>[0-5]\\d)
        (?:\\:
            (?P<seconds>
                60
                |
                [0-5]\\d
            )
        )?
    )?
    (?P<fraction>[,.][\\d.]+)?
    \\s*
    (?P<timezone>
        Z
        |
        [+-](?:
            1[0-4]
            |
            0[0-9]
        )
        (?:\\:?
            [0-5]\\d
        )?
    )?
)?
$/x