Check if a sequence of numbers corresponds to a valid timestamp

2

I need to check if a string matches a valid timestamp.
Using is_numeric , ctype_digit , will only validate if it is numeric ... I want to know if the timestamp matches a date.

    
asked by anonymous 27.07.2014 / 05:15

2 answers

3

The following response is intended to complement the answer from @gmsantos.

Using PHP's DateTime class as suggested by @gmsantos you reach your goal.

However, for a validation , you have to:

  • Use a try ... catch () block if you are programming with your Object Oriented version
  • Condition it with the boolean FALSE -OR- with the instanceof operator searching for an object DateTime , if using its procedural version date_create () :

Object Oriented

try {

    $dt = new DateTime( 1926036000 );

} catch( Exception $e ) {

    //die( $e -> getMessage() );

    die( 'Invalid date or timestamp' );
}

This is because when the constructor of the class DateTime fails it triggers an Exception that may or needs (depends on the case) to be captured.

Also, I left two ways to abort the operation. One with the error message launched by the DateTime and another custom one.

The reason is because the exception message posted by DateTime is not very useful in production:

  

DateTime :: __ construct (): Failed to parse time string (1926036000) at position 7 (0): Unexpected character

Procedural

var_dump( date_create( 1926036000 ) instanceof DateTime ); // false

var_dump( date_create() instanceof DateTime ); // true

For validate check for FALSE may be more interesting because it does not assign an assignment to a variable. If after validation the resulting object is used, prefer instanceof.

Although not shown, when I say compare by FALSE I refer to comparing the left data with the ! == operator ($ x! == FALSE), since PHP recognizes multiple values as FALSE, including the zero that can be the return of the fragment below:

mktime( 21, 0, 0, 12, 31, 1969 ); // int 0

Even if it does not make sense: P

[UPDATE]

As demonstrated by @gmsantos in the comments of this and I committed little giant bug.

The error refers to whether I'm setting timestamp directly in the DateTime builder. what is not right . Strangely, for some timestamps it "works", that is, the object is built but with a result different than expected, and for others not, firing itself to Exception.

The correct thing is to use the DateTime :: setTimestamp () method after the object is created:

$dt = new DateTime;
$dt -> setTimestamp( 2147472001 );

var_dump( $dt );
    
27.07.2014 / 16:58
4

TL; DR

Use the DateTime class of PHP and the dates represented in a format other than the timestamp in UNIX so you do not have problems with dates.

A UNIX timestamp is the number of seconds that have passed since date 1970-01-01 00:00:00 in UTC.

Your limit is at the maximum value that a 32-bit integer variable can support (-2147483648 to 2147483647), generating a range of dates between 1901-12-13 20:45:54 to 2038-01-19 03:14:07 .

That is, today just check if the number is integer and is within that range.

But 2038 is right there, we'll have a new Millennium Bug ? / strong>

If we use the datastore in Unix timestamps, yes, we have problems with the applications.

One solution to this problem is the use of 64-bit integers, which will allow the postponement of this problem for the year 292277026596 .

Today's 64-bit PHP support is experimental (At least on Windows systems) and still does not support 64-bit integers

In PHP, the best alternative is to use the class DateTime , where there is no bug of the year 2038 .

    
27.07.2014 / 14:02