6

I've tried Google, php.net and the php mailinglist's archives, but I can't find what I'm looking for. Maybe it's obvious, or maybe nobody wonders about this...

For years, I've used microtime() to get the current time including the microseconds. However, somebody pointed me at a sentence in the manual page: "This function is only available on operating systems that support the gettimeofday() system call."

And PHP's gettimeofday() suggests the same situation: "This is an interface to gettimeofday(2)."

But... what kind of systems then don't have this system call available? Some googling around provided lots of C programmers trying to get a gettimeofday() implementation in C on Windows, since it doesn't seem to include it. But PHP's microtime() and gettimeofday() seem to work just fine on Windows (at least the boxes I could get to). Also, I just can't seem to find PHP code anywhere on the web that seems to check the existence of either microtime() or gettimeofday() before they call it, and there have to be lots of PHP programmers out there working on Windows boxes so...

Should I ignore the sentence in the manual and just trust that both functions are always available? Or is there another cross-platform way to get to the system time, including the microseconds, without using microtime() or gettimeofday()? Or will both functions just always exist, but just not give me microseconds if there is no gettimeofday() system call available?

EDIT 1:

http://www.php.net/microtime

"Returns the current Unix timestamp with microseconds.
This function is only available on operating systems that 
support the gettimeofday() system call."

Maybe this clarifies my point somewhat.

lugte098
  • 2,271
  • 5
  • 21
  • 30
  • 1
    Are you really worried that your scripts might be transitioned to a Windows machine at some point? – user229044 Aug 11 '11 at 13:04
  • 2
    @meager is that not a valid concern? AFAIK portability is still a pillar of development. – Mike B Aug 11 '11 at 13:16
  • I am certain that my scripts will be used on a windows machine, because it is open source software that anyone can download and run on their server. We try to support as many server types and browsers as possible. – lugte098 Aug 11 '11 at 13:54
  • @lugte098: just curious: what you need such a precise time measurement for in a PHP app? – Tomas Aug 11 '11 at 14:06
  • @Tomas Telensky: I'm using it as a "as unique as possible" index value for allowing certain processes to store their data. When using only seconds, there could be a possiblity of 2 processes to conflict with each other when fired almost simultaneously. Their is no other possible way of doing this than with microtime() or equivalent, at least for our software. – lugte098 Aug 11 '11 at 14:15
  • @lugte098: what about using random numbers? E.g. in combination with seconds and md5? Like `md5(rand() . $seconds)`? – Tomas Aug 11 '11 at 14:28
  • @Tomas Telensky: Then it is not possible to see which process is the most recent. Trust me, we need something equivalent to microtime(). We use it also when we write log files. – lugte098 Aug 11 '11 at 14:40
  • @lugte098: OK. But according to the PHP manual there is no direct warning about gettimeofday() unportability, and vascowhite reports that it works well on windows. – Tomas Aug 11 '11 at 16:21

2 Answers2

1

According to sources PHP has its own gettimeofday implementation for Windows. But there's no other ports of gettimeofday available. So except UNIX based systems and Windows, any other operating systems cannot use microtime()

useraged
  • 1,706
  • 17
  • 34
  • Thanks for the answer, at least now i'm somewhat reassured that it will work on all our users systems. – lugte098 Aug 12 '11 at 14:27
0

I have developed extensively on both windows and linux and have working PHP applications running on both platforms. I have never been aware of this issue and it has never been a problem for me.

I would regard this as a bridge to cross if you get to it. A lot of time and energy could go into finding a solution for what seems to me to be a non problem. I reccomend that you ignore the sentence in the manual until this actually becomes an issue for you rather than wasting resources on the off chance that it could happen.

Now that you have made me aware of this issue I will follow my own advice and forget about it.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • I do not totally agree with you. If you know there is a potential bug in your software, you want to tackle the problem before it even has a chance to cause trouble. Their are situations where this HAS in fact caused some problems. Better safe than sorry i would say... – lugte098 Aug 11 '11 at 13:57
  • @vascowhite: but have you been using the gettimeofday() function also? Does it work on windows? – Tomas Aug 11 '11 at 14:05
  • @Tomas on my windows box `var_dump(gettimeofday())` gives `array(4) { ["sec"]=> int(1313077595) ["usec"]=> int(798227) ["minuteswest"]=> int(0) ["dsttime"]=> int(0) }` – vascowhite Aug 11 '11 at 15:49
  • @vascowhite: OK, thanks, it looks it works! So no need to be concerned as it seems. – Tomas Aug 11 '11 at 16:17
  • @Tomas That was on windows 7, I also run apps on win server 2008 R2 and have had no issues, but I can do the same test tomorrow and let you know. – vascowhite Aug 11 '11 at 17:02
  • So, yes it doesn't seem to be a problem of windows per se. But still there are a lot of windows machines out there (maybe only certain versions) that DO have this problem. So if there exists an equivalent of microtime/gettimeofday then i would like to use this. – lugte098 Aug 12 '11 at 09:59