English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutorial Básico PHP

Tutorial Avançado PHP

PHP & MySQL

Manual de Referência PHP

Uso e exemplo da função PHP date_timezone_get()

PHP Date & Time Function Manual

A função date_timezone_get() retorna o fuso horário em relação ao DateTime fornecido

Definição e uso

A função date_timezone_get() é um alias de DateTime::getTimezone. Ela aceita um objeto DateTime como parâmetro e retorna o fuso horário em relação à data fornecida./The timezone object of the time (object).

Syntax

date_timezone_get($object)

Parameter

Serial numberParameters and descriptions
1

object (required)

This means you need the DateTime object for the timezone.

Return value

This function returns a DateTimeZone object. If it fails, it returns a boolean valuefalse.

PHP version

This function was initially introduced in PHP version5.2.1introduced in and can be used in all higher versions.

Online example

The following isdate_timezone_get()Example of function-

<?php
   $date = date_create("25-09-1989");   
   $res = date_timezone_get($date);
   $timeZone_name = timezone_name_get($res);
   print("Timezone: ").$timeZone_name;
?>
Test and see‹/›

Output result

Timezone: UTC

Online example

The following example sets the timezone and usesdate_timezone_get()The function retrieves it.

<?php
   $tz = new DateTimeZone("Indian/Mahe"); 
   $date = date_create("25-09-1989", $tz);   
   $res = date_timezone_get($date);
   print_r($res);
?>
Test and see‹/›

Output result

DateTimeZone Object
(
    [timezone_type] => 3
    [timezone] => Indian/Mahe
)

Online example

Thisdate_timezone_get()The function just gives you the timezone object, you can get its nameUse timezone_name_get()   -

<?php
   $tz = new DateTimeZone("Indian/Mahe"); 
   $date = date_create("25-09-1989", $tz);   
   $res = date_timezone_get($date);
   $timeZone_name = timezone_name_get($res);
   print("Default timezone: ").$timeZone_name;
?>
Test and see‹/›

Output result

Default timezone: Indian/Mahe

Online example

Change the default timezone

<?php
   echo "The old timezone is ". date_default_timezone_get();
   $timeZone = 'Asia/Shanghai';
   
   if(date_default_timezone_set($timeZone)){
      #Now we get this timezone.
      echo "The new timezone is ". date_default_timezone_get();
   }
?>
Test and see‹/›

Output result:

The old timezone is UTC
The new timezone is Asia/Shanghai

PHP Date & Time Function Manual