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

6

14

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?

Helterskelter answered 24/8, 2012 at 10:32 Comment(3)
I'm always wondering that people, who "has" thousands requests per second always trying to economy on trivial functions and instead of measuring/profiling waste the time on guessings.Harden
why would you use the time for verification? Shouldn't a unique identifier for verification be as unpredictable as possible? The time can be (within limits, but still) predicted! Wouldn't it be much better to simply use a random value? (possibly with the time as seed for the random generator, but that's a side point)Personalize
Both time() and $_SERVER['REQUEST_TIME'] are the number of seconds from 1970/01/01. microtime() uses microseconds.Cobble
R
15

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.

Romola answered 24/8, 2012 at 10:35 Comment(3)
You can't rely on the time then. Maybe generate a random token and salt it with the tim and ip of the reques, this should do the trick.Romola
do $_SERVER[R_T] changes every time the form is submitted ?Helterskelter
It is not guaranteed to be unique, if the web server is threaded and receives two connections at the same time the values ill most likely be identical.Romola
K
9

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 (PHP 5.1; Nov 2005).

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_FLOAT'] is the timestamp of the start of the request, with microsecond precision (PHP 5.4; March 2012).

Kimbro answered 24/8, 2012 at 10:36 Comment(4)
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, sir, the microtime() would make PHP sleep? isn't it?Helterskelter
Microtime doesn't sleep. If you need a unique value, use something designed for that - such as php.net/manual/en/function.uniqid.php or a some other UUID generator.Kimbro
but it uses gettimeofday() which makes PHP sleep , i guess?Helterskelter
gettimeofday() takes time and some effort, but it does not deliberately wait.Kimbro
V
6

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)

Vernacularism answered 18/4, 2013 at 15:58 Comment(0)
H
2

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

Horsepowerhour answered 24/8, 2012 at 10:35 Comment(0)
F
2

you should take in consideration milliseconds based on microtime()

$milliseconds = round(microtime(true) * 1000);
Fancied answered 24/8, 2012 at 10:36 Comment(0)
A
1

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.

Actinotherapy answered 24/8, 2012 at 10:34 Comment(6)
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, sir, the microtime() would make PHP sleep? isn't it?Helterskelter
If you're looking for a unique string for verification purposes, you should probably look into the function uniquid(). It "gets a prefixed unique identifier based on the current time in microseconds".Actinotherapy
uniqid() uses gettimeofday() which makes PHP sleep? so am avoiding it , sir. :)Helterskelter
does $_SERVER[R_T] changes every time the form is submitted ?Helterskelter
Have you done enough profiling to know for sure that sleep() has any notable performance issues?Actinotherapy
yes sir, I read about it on the web itself...got an article too about its sleep , that's why avoiding it , and observed myself in my application too, in my websiteHelterskelter

© 2022 - 2024 — McMap. All rights reserved.