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?

sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • 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. – zerkms Aug 24 '12 at 10:34
  • 3
    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) – codeling Aug 24 '12 at 10:37
  • 1
    Both time() and $_SERVER['REQUEST_TIME'] are the number of seconds from 1970/01/01. microtime() uses microseconds. – lisandro Apr 16 '20 at 13:20

6 Answers6

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Intrepidd
  • 19,772
  • 6
  • 55
  • 63
  • 2
    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. – Intrepidd Aug 24 '12 at 10:39
  • do $_SERVER[R_T] changes every time the form is submitted ? – sqlchild Aug 24 '12 at 10:52
  • 1
    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. – Intrepidd Aug 24 '12 at 10:53
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).

hakre
  • 193,403
  • 52
  • 435
  • 836
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • 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? – sqlchild Aug 24 '12 at 10:37
  • 2
    Microtime doesn't sleep. If you need a unique value, use something designed for that - such as http://php.net/manual/en/function.uniqid.php or a some other UUID generator. – Alister Bulman Aug 24 '12 at 13:04
  • but it uses gettimeofday() which makes PHP sleep , i guess? – sqlchild Aug 24 '12 at 13:16
  • gettimeofday() takes time and some effort, but it does not deliberately wait. – Alister Bulman Aug 24 '12 at 13:26
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)

DayloStar
  • 151
  • 1
  • 7
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.

deed02392
  • 4,799
  • 2
  • 31
  • 48
2

you should take in consideration milliseconds based on microtime()

$milliseconds = round(microtime(true) * 1000);
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
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.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • 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? – sqlchild Aug 24 '12 at 10:35
  • 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". – Wayne Whitty Aug 24 '12 at 10:39
  • uniqid() uses gettimeofday() which makes PHP sleep? so am avoiding it , sir. :) – sqlchild Aug 24 '12 at 10:40
  • does $_SERVER[R_T] changes every time the form is submitted ? – sqlchild Aug 24 '12 at 10:42
  • Have you done enough profiling to know for sure that sleep() has any notable performance issues? – Wayne Whitty Aug 24 '12 at 10:42
  • 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 website – sqlchild Aug 24 '12 at 10:50