Timezones in PHP
Handle multiple timezones with DateTimeZone and date_default_timezone_set.
Why Timezones Matter
If your application serves users globally, timezone handling is critical. PHP uses timezone-aware functions and classes to handle conversions correctly.
- Always store times in UTC in the database
- Convert to user's local timezone only for display
date_default_timezone_set()
Set the default timezone for all date functions:
<?php
// Set at the top of your script or in php.ini
date_default_timezone_set('Europe/Istanbul');
echo date('Y-m-d H:i:s'); // local Istanbul time
echo PHP_EOL;
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s'); // UTC time
// Or set in php.ini:
// date.timezone = "Europe/London"