What to use ? time() function or $_SERVER['REQUEST_TIME'] ? Which is better?

二次信任 提交于 2019-12-29 07:27:08

问题


Which one among these two time functions is better?

I have a form on my website which is submitted by thousands of users every microsecond or less , thus , thousands of requests at the same time on my server.

So I want to use the one which does not uses any sleep while execution. Also , how unique is $_SERVER['REQUEST_TIME'] and time()? Do both change every microsecond?

Will $_SERVER['REQUEST_TIME'] change every time I submit the form? Like , the time() function will change at every time difference.

I need to give a unique url to the user for verification in which am appending the unique result generated from any one of the two? Also, using microtime() would make PHP sleep? isn't it?


回答1:


It depends.

$_SERVER['REQUEST_TIME'] corresponds to the time when the request has started, the web server provides this data.

time() actually runs a syscall in order to check the time when this line is called.

You need to be more specific on what you want to do so we can give you complete answers.




回答2:


time() changes once per second. microtime() (optionally, with (true)) more frequently.

$_SERVER['REQUEST_TIME'] is set when the PHP process starts, but it's just a simple variable lookup, and so faster. Most of the time, you don't need micro-second resolution, especially if the process is only going to be running for a fraction of a second anyway to generate a webpage.




回答3:


from php.net:

$_SERVER['REQUEST_TIME_FLOAT'];
The timestamp of the start of the request, with microsecond precision.

http://php.net/manual/en/reserved.variables.server.php (Careful though, see Alister's note about using REQUEST_TIME)




回答4:


$_SERVER['REQUEST_TIME'] is for the time that the session started, it is completely different to using time() or microtime() in that it's constant for that run.




回答5:


you should take in consideration milliseconds based on microtime()

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



回答6:


You should use time() or microtime(), or uniqid() if this needs to be unique for verification purposes.

Question is, why do you need the time to be unique?

EDIT: Because you edited your question.

$_SERVER['REQUEST_TIME'] will give you the timestamp of the start of the request. It will change according to the time the user makes a request.



来源:https://stackoverflow.com/questions/12107688/what-to-use-time-function-or-serverrequest-time-which-is-better

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!