0

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

1 Answers1

2

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

ʰᵈˑ
  • 10,738
  • 2
  • 20
  • 45
  • Very good but still have the +12 in the time take a look [h e r e](http://www.twaa9l.com/date.php) – Youssef Al Subaihi Jan 02 '15 at 13:29
  • should i use ``str_replace`` or there is someway to remove that ? – Youssef Al Subaihi Jan 02 '15 at 13:31
  • this is a good way using ``preg_replace`` BUT it will keep the 12 number at the end which it will make time wrong – Youssef Al Subaihi Jan 02 '15 at 13:37
  • You realise that the value you're trying to remove the string data from isn't actually a string but a float? – Mark Baker Jan 02 '15 at 14:17
  • @MarkBaker it was string i fixed it by using very simple code ``(int)(microtime(true)*1000);`` – Youssef Al Subaihi Jan 02 '15 at 17:20
  • Then perhaps you should advise that PHP documents team that the docs for [round()](http://www.php.net/manual/en/function.round.php) are incorrect, because that's pretty clear that you should get a __float__ returned, not a __string__: `float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )` – Mark Baker Jan 02 '15 at 18:10
  • Or if your problem is with [microtime()](http://www.php.net/manual/en/function.microtime.php) then that also should return a float if the `$get_as_float` argument is true. – Mark Baker Jan 02 '15 at 18:12