date_default_timezone_set()

Irrespective of time zone set in php.ini file , we can set the time zone of the server for our script by using date_default_timezone_set() function.
The list of applicable time zone identifier is available here.
Here is the sample code to set the time zone.
<?Php
echo date_default_timezone_set();
?>
We can set it like this
date_default_timezone_set('America/Chicago');
After setting the time zone , you can read the same by using date_default_timezone_get() function.

Setting the time zone in php.ini file

You can set the default time zone at php.ini file. If you are using for first time then search for the line saying
;date.timezone =
Change it to
date.timezone ="Asia/Calcutta";
You can change the area as per your requirement by using time zone identifier .

Example: Dynamically Switching Time Zones

date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s').'<br>'; // Output: Current time in New York
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s'); // Output: Current time in Kolkata
Output
2025-11-20 23:59:42
2025-11-21 10:29:42

Example: Listing All Available Time Zones

$timezones = timezone_identifiers_list();
print_r($timezones);
Or to display as list
$timezones = timezone_identifiers_list();
foreach($timezones as $key=>$value){
	echo "$key : $value".'<br>';
}

Creating a dropdown list for user selection of Timezone

PHP Date Functions date_default_timezone_get() get the time zone
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer