How is PHP's $_SERVER[REQUEST_TIME] set?
Asked Answered
P

1

9

What happens on the server to set the request time? Does it take into account the timezone for which the server is configured?

I'm asking because I need to know, if I have a site that has a timezone set as a site-wide variable, and I compare something to the $_SERVER['REQUEST_TIME'] to know if it's expired, I'm not sure whether a timezone mismatch is possible.

Plasmasol answered 10/6, 2013 at 16:28 Comment(0)
C
12

$_SERVER's 'REQUEST_TIME' is a Unix timestamp. That should be enough information, but if it isn't: Unix timestamps are always UTC-based.

PHP Example

The notation for Unix timestamps in DateTime is to prefix the number with the at-sign ("@"). Then the second $timeZone parameter is ingored and defaults to "UTC" because it is a Unix timestamp which are always UTC based:

$requestTime = new DateTime("@$_SERVER[REQUEST_TIME]");

Gives:

class DateTime#1 (3) {
  public $date          => string(19) "2013-06-23 07:45:44"
  public $timezone_type => int(1)
  public $timezone      => string(6) "+00:00"
}

It is not even possible to force (by timestamp at construction) the DateTime object to a different timezone - only later on changing the timezone.

Chemotherapy answered 10/6, 2013 at 16:31 Comment(3)
That's the one little piece of information I wasn't sure about. unixtimestamp.com implies that Unix timestamps are not always in UTC. Is defaulting to UTC for Unix timestamps something the PHP engine is doing?Plasmasol
No, a unix timestamp is literally the number of seconds since January 1st 1970, in UTC. So there's no real timezone associated to it, it's just the starting point they chose. Anything that uses a different starting point is not a unix timestamp.Chemotherapy
My first thought about that @ sign in front of the timestamp was that you were just silencing a PHP E_Notice because there is probably no REQUEST_TIME element in it. But after trying it out and reading the docs I can state that's correct.Stagnant

© 2022 - 2024 — McMap. All rights reserved.