Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 694555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:52:07+00:00 2026-05-14T02:52:07+00:00

I have a string and I need to find out whether it is a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:52:07+00:00Added an answer on May 14, 2026 at 2:52 am

    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
    

    On 64bit systems the integer is of course larger than that and the function will no longer return false for “2147483648” and “-2147483649” but for the corresponding larger numbers.


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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of strings. I need to find out from this collection
I have a pattern @@{} and given a string I need to find out
I have a string and I need to find out what was matched in
I have a the string 'Hello', I need to find out what characters occupy
I have this javascript in a classic asp page... I need to find out
I have a large string I need to parse, and I need to find
I have a string with markup in it which I need to find using
I have a string(char*), and i need to find its underlying datatype such as
I have a string I need to feed to a com object that has
I have a string that I need to convert to the equivalent array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.