1

How can I check that a given string is a valid timestamp in laravel. I want to define a validation for it. The input string is a timestamp by itself and does not have any format. for example "56123231"

Salar
  • 5,305
  • 7
  • 50
  • 78
  • @NicklasKevinFrank no it is not duplicate, the mentioned question is getting a formatted string, but I am asking about a timestamp string with no format. – Salar May 19 '16 at 13:31
  • "Timestamp string" already implies it's not just any old string but one that has some specific format. – ivan_pozdeev May 19 '16 at 17:48
  • So this is unspecific unless you provide a spec for the format (implying a UNIX timestamp - is it 32-bit or 64-bit? signed/unsigned? If 64-bit, is you PHP 32-bit?) – ivan_pozdeev May 19 '16 at 18:18
  • In any case, the function referred to in the duplicate has a fitting format specifier, `U`. – ivan_pozdeev May 19 '16 at 18:23

1 Answers1

3

This problem is not related to Laravel. This solution suits for any framework\library

function isValidTimestamp($timestamp)
{
   return ((string) (int) $timestamp === $timestamp)
       && ($timestamp <= PHP_INT_MAX)
       && ($timestamp >= ~PHP_INT_MAX);
}
xAoc
  • 3,448
  • 3
  • 23
  • 36