Asked  7 Months ago    Answers:  5   Viewed   34 times

I have a string and I need to find out whether it is a unix timestamp or not, how can I do that effectively?

I found this thread via Google, but it doesn't come up with a very solid answer, I'm afraid. (And yes, I cribbed the question from the original poster on the aforementioned thread).



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

Wednesday, March 31, 2021
answered 7 Months ago

Your date format is wrong... i is for minute, not m (months).

return date("Y-m-d H:i:s", $unixTimestamp);

A few side notes:

  • There's no need to re-assign, i.e. $unixTimestamp = $unixTimestamp;
  • Since you're using PHP > 5.3. you may be interested in the new DateTime object.
Wednesday, March 31, 2021
answered 7 Months ago



    $time = time();

    $sql = "INSERT INTO yourtablename (ArrivalTime) Values ('$time')";


P.S: in the sql statement i'm sure you'll need to put other things in the other fields ,so you just replace the one above by this:

$sql = "INSERT INTO yourtablename (field1, field2, ArrivalTime) Values ('$value1','$value2','$time')";



$error = "Couldn't connect";
$connect = mysql_connect("localhost","username","password") or die($error);
mysql_select_db("yourdatabase") or die($error);

Wednesday, March 31, 2021
answered 7 Months ago

What about isEmpty() ?

if(str != null && !str.isEmpty())

Be sure to use the parts of && in this order, because java will not proceed to evaluate the second part if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.

Beware, it's only available since Java SE 1.6. You have to check str.length() == 0 on previous versions.

To ignore whitespace as well:

if(str != null && !str.trim().isEmpty())

(since Java 11 str.trim().isEmpty() can be reduced to str.isBlank() which will also test for other Unicode white spaces)

Wrapped in a handy function:

public static boolean empty( final String s ) {
  // Null-safe, short-circuit evaluation.
  return s == null || s.trim().isEmpty();


if( !empty( str ) )
Tuesday, June 1, 2021
answered 5 Months ago

To find out if a string contains substring you can use the index function:

if (index($str, $substr) != -1) {
    print "$str contains $substrn";

It will return the position of the first occurrence of $substr in $str, or -1 if the substring is not found.

Wednesday, June 16, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :