maxxd Posted November 28, 2020 Share Posted November 28, 2020 (edited) 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 November 28, 2020 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/311750-rest-api-caching/page/2/#findComment-1582682 Share on other sites More sharing options...
kicken Posted November 28, 2020 Share Posted November 28, 2020 (edited) 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 November 28, 2020 by kicken 1 Quote Link to comment https://forums.phpfreaks.com/topic/311750-rest-api-caching/page/2/#findComment-1582683 Share on other sites More sharing options...
maxxd Posted November 28, 2020 Share Posted November 28, 2020 Interesting - don't know how I've managed to avoid that issue. Thanks for the info! Quote Link to comment https://forums.phpfreaks.com/topic/311750-rest-api-caching/page/2/#findComment-1582688 Share on other sites More sharing options...
NotionCommotion Posted November 28, 2020 Share Posted November 28, 2020 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) Quote Link to comment https://forums.phpfreaks.com/topic/311750-rest-api-caching/page/2/#findComment-1582692 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.