2
I need to check if a sequence matches a valid timestamp.
Use is_numeric
, ctype_digit
, will only validate if it is numerical... I want to know if the timestamp matches a date.
2
I need to check if a sequence matches a valid timestamp.
Use is_numeric
, ctype_digit
, will only validate if it is numerical... I want to know if the timestamp matches a date.
4
TL;DR
Use the class DateTime
PHP and dates represented in a different format than UNIX timestamp to not have problems with dates.
A UNIX timestamp is the number of seconds that have passed since the date 1970-01-01 00:00:00
in UTC.
Its limit is at most value that a 32-bit integer variable can support (-2147483648 to 2147483647), generating a date range between 1901-12-13 20:45:54
until 2038-01-19 03:14:07
.
That is to say, today just check if the number is integer and is within that range.
But 2038 is coming soon, we’ll have a new "Millennium bug" ?
If we use date storage on Unix timestamps, yes, we will have application problems.
One solution to this problem is the use of 64-bit integers, which will make it possible to postpone this problem to the year 292277026596.
Today 64-bit PHP support is experimental (At least on Windows systems) and still nay supports 64-bit integers
In PHP, the best alternative is to use the class DateTime
, where there is no such bug of the year 2038.
Interestingly, I didn’t know Datetime didn’t suffer from this boundary problem.
@Blackout does not suffer if the input that creates the date is not set to timestamp.
I’m not sure I understand your comment
2147472001 - timestamp.. 2038-01-18 22:00:01 - format Y-m-d H-i-s
I tested it now... You don’t suffer from this particular bug but 2147472001 that should be 18/01/2038 - 22:00:01 eventually becomes 2001-07-27 21:47:47 o. The
@gmsantos Good explanation. I hope that by 2038 we already have security in using it
@Brunoaugusto - http://3v4l.org/7nIOS
Ahhh... My fault. I was passing the timestamp in the constructor. I didn’t know this method Date::setTimestamp().
Here the link example returned: 2038-01-18 22:00:01 -02:00
Maybe you got confused about the error presented. I edited the fragment including a call to date_default_timezone_set()
and no errors were made, just as the Pope’s.
What error @Runo ?
As for the fact that no Timezone has been set. That it is not safe to trust the timmezone of PHP.INI and blah, blah, blah... : P
This is a hhvm error, not PHP.
3
The following response is intended to complement the @gmsantos response.
Using the class Datetime PHP as suggested by @gmsantos you achieve your goal.
However, for a validation, you have to:
Object-Oriented
try {
$dt = new DateTime( 1926036000 );
} catch( Exception $e ) {
//die( $e -> getMessage() );
die( 'Invalid date or timestamp' );
}
That’s because when the class builder Datetime fail it fires an Exception that can or needs (depending on the case) to be captured.
Note also that I left two ways to abort the operation. One with the error message sent by Datetime and other personalized.
The reason is because the exception message launched by Datetime is not very useful in production:
Datetime::__Construct(): Failed to parse time string (1926036000) at position 7 (0): Unexpected
Procedural
var_dump( date_create( 1926036000 ) instanceof DateTime ); // false
var_dump( date_create() instanceof DateTime ); // true
To validate check by FALSE can be more interesting by dispensing an assignment to a variable. If after validation the resulting object is used, prefer instanceof.
Although not demonstrated, when I say compare by FALSE I mean compare the data on the left with the operator !== ( $x !== FALSE ), since PHP recognizes several 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 doesn’t make sense :P
[UPDATE]
As demonstrated by @gmsantos in the comments of this and another topic I made little giant mistake.
The error refers to me being setting the timestamp directly in the builder of Datetime what it’s not right. Strangely, for some timestamps "works", that is, the object is built but with a different result than expected, and for others not, shooting the Exception.
The correct is to use even the method Date::setTimestamp() after creating the object:
$dt = new DateTime;
$dt -> setTimestamp( 2147472001 );
var_dump( $dt );
I liked the example, it is right on the line as I use. I will run the tests
Its functions will always give error. Neither the constructor of the DateTime
so little date_create
accept Timestamp Unix as a parameter: http://php.net/manual/en/datetime.formats.php.
true, I saw the page and the only foot example using @TS
DateTime::__construct(): Failed to parse time string (946684800)
I supplemented the answer. For some timestamps the object is created, but with value incompatible with what the integer should represent. For others, Exception is launched right away.
Browser other questions tagged php timestamp
You are not signed in. Login or sign up in order to post.
Have you tried using [tag:regex]?
– ptkato
It’s the same, it’ll only validate if it’s a number...
– Papa Charlie
No, you can check if it is in the format of a valid timestamp with regex. Put what timestamp you want so the staff can help you.
– ptkato
That’s a valid TS
1406431728
, That’s an invalid TS9999999999
- I want to know if TS matches a date– Papa Charlie
What is the criterion for determining whether a timestamp is valid? The class
DateTime
PHP can generate a date with9999999999
. However, you cannot return the timestamp of that date...– bfavaretto
The limit on a TS is 13 Dec 1901 to 19-01-2038, does it check? Then a criterion can be the TS within these 2 tracks and then try to create a date with the TS
– Papa Charlie
Regex is unnecessary in this case. Since timestamp is an integer numeric value, simply set the ranges to >= and <=
– gmsantos