Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP DateTimeZone Class

The DateTimeZone class is responsible for representing a timezone.

Below we can see the class description, which shows its constants as well as its methods.

Description / Descripción
 class DateTimeZone {
/* Constants */
const int AFRICA = 1;
const int AMERICA = 2;
const int ANTARCTICA = 4;
const int ARCTIC = 8;
const int ASIA = 16;
const int ATLANTIC = 32;
const int AUSTRALIA = 64;
const int EUROPE = 128;
const int INDIAN = 256;
const int PACIFIC = 512;
const int UTC = 1024;
const int ALL = 2047;
const int ALL_WITH_BC = 4095;
const int PER_COUNTRY = 4096;
/* Methods */
public __construct(string $timezone)
public getLocation(): array|false
public getName(): string
public getOffset(DateTimeInterface $datetime): int
public getTransitions(int $timestampBegin = PHP_INT_MIN, int $timestampEnd = PHP_INT_MAX): array|false
public static listAbbreviations(): array
public static listIdentifiers(int $timezoneGroup = DateTimeZone::ALL, ?string $countryCode = null): array
In this post, we will use $obj as a reference to the object created with the class.


Let’s make a small example with a constant:

getting the zone constat
$zone = DateTimeZone::AFRICA;
echo $zone;

OK, but this doesn’t seem too useful, where do we use the constants?

Let’s do a second example, this time creating a new DateTimeZone.

getting all Africa Timezone
$d = new DateTimeZone("Africa/Abidjan");
echo var_dump($thelist);
array(52) {
  string(14) "Africa/Abidjan"
  string(12) "Africa/Accra"
  string(18) "Africa/Addis_Ababa"
  string(14) "Africa/Algiers"
  string(13) "Africa/Asmara"
  string(13) "Africa/Bamako"
  string(13) "Africa/Bangui"
  string(13) "Africa/Banjul"
  string(13) "Africa/Bissau"
  string(15) "Africa/Blantyre"
  string(18) "Africa/Brazzaville"
  string(16) "Africa/Bujumbura"
  string(12) "Africa/Cairo"
  string(17) "Africa/Casablanca"
  string(12) "Africa/Ceuta"
  string(14) "Africa/Conakry"
  string(12) "Africa/Dakar"
  string(20) "Africa/Dar_es_Salaam"
  string(15) "Africa/Djibouti"
  string(13) "Africa/Douala"
  string(15) "Africa/El_Aaiun"
  string(15) "Africa/Freetown"
  string(15) "Africa/Gaborone"
  string(13) "Africa/Harare"
  string(19) "Africa/Johannesburg"
  string(11) "Africa/Juba"
  string(14) "Africa/Kampala"
  string(15) "Africa/Khartoum"
  string(13) "Africa/Kigali"
  string(15) "Africa/Kinshasa"
  string(12) "Africa/Lagos"
  string(17) "Africa/Libreville"
  string(11) "Africa/Lome"
  string(13) "Africa/Luanda"
  string(17) "Africa/Lubumbashi"
  string(13) "Africa/Lusaka"
  string(13) "Africa/Malabo"
  string(13) "Africa/Maputo"
  string(13) "Africa/Maseru"
  string(14) "Africa/Mbabane"
  string(16) "Africa/Mogadishu"
  string(15) "Africa/Monrovia"
  string(14) "Africa/Nairobi"
  string(15) "Africa/Ndjamena"
  string(13) "Africa/Niamey"
  string(17) "Africa/Nouakchott"
  string(18) "Africa/Ouagadougou"
  string(17) "Africa/Porto-Novo"
  string(15) "Africa/Sao_Tome"
  string(14) "Africa/Tripoli"
  string(12) "Africa/Tunis"
  string(15) "Africa/Windhoek"

Lista de Constantes

  • DateTimeZone::AFRICA
    • Africa time zones.
  • DateTimeZone::AMERICA
    • America time zones.
  • DateTimeZone::ANTARCTICA
    • Antarctica time zones.
  • DateTimeZone::ARCTIC
    • Arctic time zones.
  • DateTimeZone::ASIA
    • Asia time zones.
  • DateTimeZone::ATLANTIC
    • Atlantic time zones.
  • DateTimeZone::AUSTRALIA
    • Australia time zones.
  • DateTimeZone::EUROPE
    • Europe time zones.
  • DateTimeZone::INDIAN
    • Indian time zones.
  • DateTimeZone::PACIFIC
    • Pacific time zones.
  • DateTimeZone::UTC
    • UTC time zones.
  • DateTimeZone::ALL
    • All time zones.
  • DateTimeZone::ALL_WITH_BC
    • All time zones including backwards compatible.
  • DateTimeZone::PER_COUNTRY
    • Time zones per country.

I think that’s enough to understand the constants, let’s now take a look at the methods


In this section, we will see the different methods that DateTimeZone has.


The constructor method has one mandatory argument, which creates a new DateTimeZone object. $timezone must be a string and can take the following values: timezone name, offset value like "+0200", "-0100", or timezone abbreviation like "BST", "CEST", etc.

public DateTimeZone::__construct(string $timezone)

I won’t provide examples, since they were already mentioned in the constants section.


Returns information about a timezone

public DateTimeZone::getLocation(): array

Returns an array containing information about the timezone, or false on failure.

getLocation Example
$tz = new DateTimeZone("America/Costa_Rica");
    [country_code] => CR
    [latitude] => 9.93333
    [longitude] => -84.08334
    [comments] => 

Although this method may not seem useful, knowing the country code can be really helpful.


Returns the timezone name as a string

public DateTimeZone::getName(): string
getName method exampe
$tz = new DateTimeZone("America/Costa_Rica");


Returns the timezone’s GMT offset in seconds from UTC (Coordinated Universal Time), also known as Greenwich Mean Time (GMT) offset.

public DateTimeZone::getOffset(DateTime $datetime): int

In the PHP website there’s an example about this, but I’ve modified it a bit to use Uruguay and Spain as reference countries. Spain is 2 hours ahead of GMT, and Uruguay is 3 hours behind at the time of writing this post


$dateTimeZoneMontevideo = new DateTimeZone("America/Montevideo");
$dateTimeZoneMadrid = new DateTimeZone("Europe/Madrid");

$dateTimeMontevideo = new DateTime("now", $dateTimeZoneMontevideo);
$dateTimeMadrid = new DateTime("now", $dateTimeZoneMadrid);

$timeOffset = $dateTimeZoneMadrid->getOffset($dateTimeMontevideo);
$timeOffset2= $dateTimeZoneMontevideo->getOffset($dateTimeMadrid);


We can see that this integer represents the hours of Spain and Uruguay in seconds, since 7200 ÷ 60 ÷ 60 = 2 and -10800 ÷ 60 ÷ 60 = -3


This method returns the transitions of the timezone. Keep in mind that many countries have multiple timezones cause of the DST(Daylight Saving Time). For example, Uruguay is 3 hours behind GMT, but during summer this time changes.

method description
public DateTimeZone::getTransitions(int $timestampBegin = PHP_INT_MIN, int $timestampEnd = PHP_INT_MAX): array|false

The method description shows that we have a start timestamp and an end timestamp. With them, we define the period in which we want to obtain the transitions.

getTransition Example
$zona_horaria = new DateTimeZone("America/Montevideo");
$transiciones = $zona_horaria->getTransitions(631152000,PHP_INT_MAX);//631152000 se refiere al año 1990
print_r(array_slice($transiciones, 0, 3));

This would be the first 3 transitions since 1990.


Returns an associative array containing the DST(Daylight Saving Time), offset, and the timezone name

public static DateTimeZone::listAbbreviations(): array

For Example:

getting all abbreviations from Australia
$timezone_abbreviations = DateTimeZone::listAbbreviations();

In this example, all the abbreviations for ACST (Australian Central Standard Time) will be obtained.


This returns a numerically indexed array containing all the defined time zone identifiers.

public static DateTimeZone::listIdentifiers(int $timezoneGroup = DateTimeZone::ALL, ?string $countryCode = null)

We have already done an example of listidentifiers before in constants, but I made a mistake that you should not make and that is that I called the method with -> instead of :: although it worked anyway, this does not always work.

in PHP you use the scope resolution operator :: to call static methods or access static properties

Let’s see a simple example with the identifiers of Australia.

listIdentifiers example
$timezone_identifiers = DateTimeZone::listIdentifiers(DateTimeZone::AUSTRALIA);
array(11) {
  string(18) "Australia/Adelaide"
  string(18) "Australia/Brisbane"
  string(21) "Australia/Broken_Hill"
  string(16) "Australia/Darwin"
  string(15) "Australia/Eucla"
  string(16) "Australia/Hobart"
  string(18) "Australia/Lindeman"
  string(19) "Australia/Lord_Howe"
  string(19) "Australia/Melbourne"
  string(15) "Australia/Perth"
  string(16) "Australia/Sydney"
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category