4

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
mjsilva
  • 1,327
  • 3
  • 13
  • 21
  • The requirement of the OP in the linked question was to validate whether the **string** was a valid timestamp. Your first example passes an **integer**, so it will return false obviously. – Gordon May 23 '12 at 16:27

5 Answers5

5

Here, the first should be TRUE and the second should be FALSE

Why? 20101108 is a valid UNIX timestamp, - as you say, it's August 21, 1970. Could well be a person's birthday for example.

The only way to achieve what you want is to set a range of dates that mark a "sane" timestamp by the definition you are working with - e.g. anything after January 1st, 2000 - and do a check against that.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Yes, you are right, I've saw the 1970 and I assume it was wrong, my bad. – mjsilva Nov 08 '10 at 11:59
  • @mjsilva I understand your requirement, but I think you have to make sure no `yyyymmdd` type date makes it into your system in the first place. There is no reliable way to tell timestamps and date strings apart – Pekka Nov 08 '10 at 12:00
4

You can check unix timestamp format using regular expression when your timestamp is bigger than size of integer:

/**
 * Check if timestamp is in unix format.
 * 
 * @param string $timestamp
 *   Target timestamp that will be checked.
 * 
 * @return bool
 *   TRUE if timestamp is in unix format. FALSE on failure.
 */
function is_unix_timestamp($timestamp) {
   return preg_match('/^\d+$/', $timestamp);
}
milkovsky
  • 8,772
  • 4
  • 28
  • 32
  • 1
    Sweet Lord, no. There is no need to use regex. `return ctype_digit ($timestamp)` seems a logical/direct call. – mickmackusa Dec 07 '17 at 14:06
4

A workaround for this would be to check if strtotime recognises the string as a valid date string.

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

This will weed out strings that are probably date strings rather than timestamps. It is probably easier to code than writing your own sanity checks.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

Write your own ;)

Some things you could check:

  • Is the given parameter an int? if not => false
  • Is it negative or above max? => false
  • Everything else is true

This would work for your given tests.

ZeissS
  • 11,867
  • 4
  • 35
  • 50
  • Wouldn't work: the OP would like to recognize `20101108` as a date string, not a timestamp – Pekka Nov 08 '10 at 11:59
  • @Pekka: `20101108` is given as a string, not an int, thus it would return `FALSE` as requested. – ZeissS Nov 08 '10 at 12:05
-1

Any number is potentially a timestamp. The only significant difference between your two examples is that the latter case is a string. In which case:

function is_timestamp($timestamp) {
    return is_int($timestamp) || is_float($timestamp);
}

(is_numeric is no good as it also returns true for strings that can be parsed as numbers.)

bobince
  • 528,062
  • 107
  • 651
  • 834