Search code examples
phpmicrotime

remove all strings from microtime and add minutes to it


im usng this code to get php microtime

$date = round(microtime(true) * 1000);

the echo result i get like this

1.42020381242E+12

how do i make sure the microtime is just numbers no special characters or strings like this

1420209804538

on the localhost everything was ok and getting just numbers BUT on the server it's gettings numbers and String and a dot(.)

Question 2

also how do i added more 15 minute to the current microtime


Solution

  • You can use filter_var to sanitize the variable to only hold numerical values, using FILTER_SANITIZE_NUMBER_INT

    For example;

    echo filter_var($date, FILTER_SANITIZE_NUMBER_INT); 
    

    Using Mark Bakers comment, to add 15 minutes, you would just do the following;

    echo filter_var( ($date + 15 * 60 * 1000), FILTER_SANITIZE_NUMBER_INT);
    

    https://eval.in/238951

    Edit

    You can use a regular expression to remove any characters that are not numeric. For example

    preg_replace("/[^0-9]/", "", $date);
    

    Edit 2

    You can capture the last part of the string by using the following regular expression

    $re = "~\s(\d+)~"; 
    $str = "12 + 142020602353 "; 
    preg_match($re, $str, $m);
    echo $m[0];
    

    https://eval.in/238955