0

How can I list each timestamp from each timezone. I tried following code:

$timezones = DateTimeZone::listIdentifiers();

foreach ($timezones as $timezone) {
  $dt = new DateTime();
  $dt->setTimezone(new DateTimeZone($timezone));
  echo $dt->getTimestamp() . '<br />';
}

For now every loop outputs the same timestamp.

  • Of course it does. Unix Time is timezone agnostic. That's the entire point of a Unix Timestamp. – Sherif Dec 31 '19 at 00:54
  • This seems to have been asked many times before, and there is a long thread on unix timestamps here : https://stackoverflow.com/questions/23062515/do-unix-timestamps-change-across-timezones – Frank H Dec 31 '19 at 02:07

1 Answers1

3

Of course, a Unix Timestamp is timezone agnostic. That's actually one of the major benefits of Unix Timestamps. They ignore timezones completely :)

In order to see the different timezones effecting the date here you must actually look at the formatted date, not the Unix Timestamp.

$timezones = DateTimeZone::listIdentifiers();

foreach ($timezones as $timezone) {
  $dt = new DateTime();
  $dt->setTimezone(new DateTimeZone($timezone));
  echo $dt->format("Y-m-d H:i:s") . '<br />';
}
Sherif
  • 11,786
  • 3
  • 32
  • 57
  • Nitpick: Unix timestamps don't ignore timezones, they are _always UTC_. – Sammitch Dec 31 '19 at 00:59
  • i.e. they ***ignore*** timezones :) You must read it in context. "*a Unix Timestamp is timezone agnostic*" ... therefore `DateTime` objects will necessarily ignore the `DateTimezone` when giving you the timestamp, because it's not needed since it's always going to be in UTC. – Sherif Dec 31 '19 at 00:59
  • *Technically*, UTC isn't a time zone but rather a system of timekeeping. However, in practice, there is always a time zone object or setting that is labeled UTC. Thus, you are both correct depending on how you look at the problem. :) – Matt Johnson-Pint Dec 31 '19 at 18:35