Jump to content

Recommended Posts

6 hours ago, Barand said:

Have you considered RTFM?

I have, and it was a genuine question. I guess I've just been lucky enough to have never come across a situation where the distinction has bitten me in the butt or where I've noticed, in all honesty.

Edited by maxxd

Well, this would be one of those times.  You can't do simple math like $now - $last with the value returned by microtime().  You need to use microtime(true) for that.

For example:

<?php 

$last = microtime();
sleep(5);
$now = microtime();

$diff = $now - $last;
printf("%0.4f seconds have passed", $diff);

One might expect since the script sleeps for 5 seconds to get a result like 5.xxxx seconds have passed but what you actually get is:

Notice: A non well formed numeric value encountered in W:\expired.php on line 7

Call Stack:
    0.0002     391984   1. {main}() W:\expired.php:0


Notice: A non well formed numeric value encountered in W:\expired.php on line 7

Call Stack:
    0.0002     391984   1. {main}() W:\expired.php:0

0.0044 seconds have passed

Due to how microtime returns it's result $diff would never be greater than 1, and could potentially be negative.

 

Edited by kicken
  • Like 1
14 hours ago, kicken said:

Well, this would be one of those times.  You can't do simple math like $now - $last with the value returned by microtime().  You need to use microtime(true) for that.

I never understood the use for the default microtime() and even less understood why it is the default, and always pass the true argument to it.

$timeFloat = microtime(true);
$timeString = microtime();
var_dump($timeFloat);
var_dump($timeString);

$p=explode(' ', $timeString);
var_dump($p[1]+$p[0]);
Quote

float(1606594262.3616)
string(21) "0.36156100 1606594262"
float(1606594262.3616)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.