Detect User Timezone in PHP



To detect timezone, we can either set a timezone and display or we can directly display. Here, we will see two examples ?

  • Get and Display the default timezone.
  • Set a Timezone and Display it.

Get the Default TimeZone

To get the default timezone in PHP, we will use the following method ?

date_default_timezone_get(); 

Example

Let us see the example ?

<?php echo "Default TimeZone: "; echo date_default_timezone_get(); ?>

Output

Default TimeZone:UTC

Set a TimeZone and Display

In this example, we will first set a timezone using the following method ?

date_default_timezone_set

To get the timezone we have set above, use the following method ?

date_default_timezone_get(); 

Example

Let us now see the complete example ?


<?php echo "Set Default TimeZone"; date_default_timezone_set("Asia/Kolkata"); echo "
Updated TimeZone: "
; echo date_default_timezone_get(); ?>

Output

Set Default TimeZone:
Updated TimeZone: Asia/Kolkata
Updated on: 2022-11-22T06:26:55+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements