- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to 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
Advertisements