1

Possible Duplicate:
Check whether the string is a unix timestamp

i need validate a unix time with regexp on php. Example:

<?php
is_unix( $unix )
{
    if( preg_match( '/([0-9]+)/' , $unix ) )
    {
        return true;
    }
    return false;
}
?>

Thanks ;)

Community
  • 1
  • 1
Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73
  • 3
    Any number between zero and whatever `time()` produces is valid – John Conde May 23 '12 at 16:22
  • Your RegEx only checks if there's at least a single occurance of a digit in your variable. To check the whole value you can use `/^\d+$/` (to make sure it checks from the beginning of the string till the end) or cast the value into an int and check if both are equal: `(int)$unix == $unix` – inhan May 23 '12 at 16:31

1 Answers1

4

How about:

function is_unix($t) {
    if ( is_numeric($t) && 0<$t && $t<strtotime("some date in the future that you don't expect the value to exceed, but before year 2038") ) {
        return true;
    }else{
        return false;
}
dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • 1
    replace `some date in the future that you don't expect the value to exceed` with [the year 2038](http://en.wikipedia.org/wiki/Year_2038_problem)? – Mark Tomlin May 23 '12 at 16:26