Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bmg1/7fd729f6d7c9b7cbdc3e2bee8a09d7c0 to your computer and use it in GitHub Desktop.
Save bmg1/7fd729f6d7c9b7cbdc3e2bee8a09d7c0 to your computer and use it in GitHub Desktop.
Validate if a Unix timestamp is valid.
/**
* Validate if a Unix timestamp is valid.
*
* @param $unixtimestamp
* @return bool
*/
function datetime_IsUnixTimeStampValid($unixtimestamp) {
//
//
///////////////////////////////////////////////////////////////////////
// Validate Date is between start and end of 32-bit Unix timestamps
// ref: https://stackoverflow.com/a/4684066/797620
///////////////////////////////////////////////////////////////////////
//
if ( !filter_var($unixtimestamp, FILTER_VALIDATE_INT, ['options' => ['min_range' => (int)0, 'max_range' => (int)2147483647]]) ) {
//
return false;
//
}
//
//
///////////////////////////////////////////////////////////////////////
// Validate
// ref: https://stackoverflow.com/a/2524761/797620
///////////////////////////////////////////////////////////////////////
//
return ((string) (int) $unixtimestamp === $unixtimestamp) && ($unixtimestamp <= PHP_INT_MAX)&& ($unixtimestamp >= ~PHP_INT_MAX);
//
}
@bmg1
Copy link
Author

bmg1 commented Jul 8, 2021

Test and check unix time stamp in php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment