php 用什么?time() 函数还是 $_SERVER['REQUEST_TIME'] ?哪个更好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12107688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:50:20  来源:igfitidea点击:

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

phpperformancetimereliability

提问by sqlchild

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?

所以我想使用在执行时不使用任何睡眠的那个。还有,$_SERVER['REQUEST_TIME']and有多独特time()?两者是否每微秒都在变化?

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

每次提交表单时,$_SERVER['REQUEST_TIME'] 都会改变吗?就像 , time() 函数会在每个时差发生变化。

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?

我需要为用户提供一个唯一的 url 以进行验证,其中附加了从两者中的任何一个生成的唯一结果?另外,使用 microtime() 会使 PHP 休眠吗?不是吗?

回答by Intrepidd

It depends.

这取决于。

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

$_SERVER['REQUEST_TIME']对应于请求开始的时间,Web 服务器提供此数据。

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

time()实际上运行系统调用以检查调用此行的时间。

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

您需要更具体地说明您想做什么,以便我们可以为您提供完整的答案。

回答by Alister Bulman

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

time()每秒变化一次。 microtime()(可选地,使用(true))更频繁。

$_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.

$_SERVER['REQUEST_TIME']在 PHP 进程启动时设置,但它只是一个简单的变量查找,因此速度更快。大多数情况下,您不需要微秒级分辨率,特别是如果该进程只需要运行几分之一秒即可生成网页。

回答by DayloStar

from php.net:

来自 php.net:

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

$_SERVER['REQUEST_TIME_FLOAT'];
请求开始的时间戳,精度为微秒。

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

http://php.net/manual/en/reserved.variables.server.php(小心,请参阅 Alister 关于使用 REQUEST_TIME 的说明)

回答by deed02392

$_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.

$_SERVER['REQUEST_TIME']是针对会话开始的时间,它与使用完全不同,time()或者microtime()在该运行中它是恒定的。

回答by Mihai Iorga

you should take in consideration milliseconds based on microtime()

您应该考虑基于microtime() 的毫秒数

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

回答by Wayne Whitty

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

如果出于验证目的需要唯一,则应使用 time() 或 microtime() 或 uniqid()。

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.

$_SERVER['REQUEST_TIME'] 会给你请求开始的时间戳。它会根据用户发出请求的时间而变化。