Check whether the string is a unix timestamp
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
Checking if am receiving a valid unix timestamp
I'd recommend Moment.js for everything date related in JS
Especially the isValid()
method is of interest
console.log("12-12-1212", moment("12-12-1212", 'YYYY-MM-DD', true).format(), moment("12-12-1212", 'YYYY-MM-DD', true).isValid())
console.log("1212-12-12", moment("1212-12-12", 'YYYY-MM-DD', true).format(), moment("1212-12-12", 'YYYY-MM-DD', true).isValid())
console.log("1212-12-1212", moment("1212-12-1212", 'YYYY-MM-DD', true).format(), moment("1212-12-1212", 'YYYY-MM-DD', true).isValid())
console.log('1318874398', moment('1318874398', 'X', true).format(), moment('1318874398', 'X', true).isValid())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
$var is valid unix timestamp
Here, the first should be TRUE and the second should be FALSE
Why? 20101108
is a valid UNIX timestamp, - as you say, it's August 21, 1970. Could well be a person's birthday for example.
The only way to achieve what you want is to set a range of dates that mark a "sane" timestamp by the definition you are working with - e.g. anything after January 1st, 2000 - and do a check against that.
JestJS: How to test for unix timestamp
First, be sure you use \d
in your regex to match digits, not d
which only matches the actual letter.
If that doesn't solve the issue, convert the value to a string:
expect(1509736771.toString()).toMatch(/^\d{10}$/)
or
expect('' + 1509736771).toMatch(/^\d{10}$/)
How to identify a unix timestamp from a date?
Maybe naive, but a solution might be to check if $data has a numeric value.
if (is_numeric($data)) { // it's a timestamp
$data = date("d-m-Y H:i:s", (int) $data);
}
strtotime and checking if valid timestamp
'Cause strtotime returns int
and you are comparing it strictly with string. Change this (string) (int) $timestamp === $timestamp
to this (int)(string) $timestamp === $timestamp
Related Topics
Php: Inserting Values from the Form into MySQL
PHP Link to Image File Outside Default Web Directory
How to Check If a Video Exists on Youtube, Using PHP
Read a File Backwards Line by Line Using Fseek
Stop Caching for PHP 5.5.3 in Mamp
Passing an Array Using an HTML Form Hidden Element
Add a Prefix to Each Item of a PHP Array
How to Make a String in PHP with a Backslash in It
How to Remove Square Brackets and Anything Between Them with a Regex
Seamless Way to Check If User Likes Page
Create a Dynamic MySQL Query Using PHP Variables
What Can Use for Datetime::Diff() for PHP 5.2
Sqlsrv_Num_Rows Not Returning Any Value