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.

hakre
  • 193,403
  • 52
  • 435
  • 836
beth
  • 1,916
  • 4
  • 23
  • 39

1 Answers1

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.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • That's the one little piece of information I wasn't sure about. http://www.unixtimestamp.com/ implies that Unix timestamps are not always in UTC. Is defaulting to UTC for Unix timestamps something the PHP engine is doing? – beth Jun 10 '13 at 16:34
  • 1
    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. – Evert Jun 10 '13 at 17:40
  • 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](https://www.php.net/manual/en/datetime.construct.php#refsect1-datetime.construct-parameters) I can state that's correct. – Anse Sep 13 '19 at 03:49