SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function isTimestamp$string ) {
        return ( 
    === preg_match'~^[1-9][0-9]*$~'$string ) );


  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for that. One more question, what if I have integer variable and want to check whether it is timestamp?

  5. #5
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    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.

  6. #6
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'test' isn't a valid unix timestamp, why would it be valid? It has no meaning as timestamp.

  7. #7
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by acid24ro View Post
    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.
    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.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by acid24ro View Post
    Just check that is a positive integer (greather than or equal to 0).



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

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

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


  10. #10
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unix timestamps aren't always 10 digits long, remember.

  11. #11
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SJH View Post
    Unix timestamps aren't always 10 digits long, remember.
    Indeed. Even 2000 can also be correct unix time stamp and you can get date:
    PHP Code:
    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:
    Code:
    1970-01-01 00:38:20
    So starting from 0 unix time stamp is epoch from 1970.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  12. #12
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

    PHP Code:
    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.
    CanisHosting - Web Hosting plans starting at $3.95 per month

  13. #13
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No the unix timestamp is the output of time() function - so it is always 10 digits long.

  14. #14
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    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?
    CanisHosting - Web Hosting plans starting at $3.95 per month

  15. #15
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    CanisHosting - Web Hosting plans starting at $3.95 per month

  16. #16
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sparek View Post
    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?
    Ok then... but that means there is no method how to tell the normal integer from unix timestamp...

  17. #17
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    CanisHosting - Web Hosting plans starting at $3.95 per month

  18. #18
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sparek View Post
    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:

    PHP Code:
    if (strlen($variable) >= 10
    I won't work with very high integers.

  19. #19
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Instead of using Unix timestamp why not use the better alternative ISO 8601?
    http://en.wikipedia.org/wiki/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
    Code:
    /^
    (?:
        (?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
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •