PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Setting the timezone in PHP can be achieved in a few different ways. Here are a few common methods:
1. Using the date_default_timezone_set()
function:
The date_default_timezone_set()
function is used to set the default timezone used by all date/time functions in a script.
Here's an example of how to use it:
<?php date_default_timezone_set('America/New_York'); echo 'The current date/time in New York is: ' . date('Y-m-d H:i:s'); ?>
In the above script, 'America/New_York' is the timezone being set, and date('Y-m-d H:i:s')
outputs the current date and time in the 'Y-m-d H:i:s' format.
You can replace 'America/New_York' with any valid timezone identifier from this list: https://www.php.net/manual/en/timezones.php.
2. Using the ini_set()
function:
The ini_set()
function is used to set configuration options for a PHP script. You can use it to set the date.timezone
configuration option, which is the default timezone used by all date/time functions.
Here's an example of how to use it:
<?php ini_set('date.timezone', 'Europe/London'); echo 'The current date/time in London is: ' . date('Y-m-d H:i:s'); ?>
In the above script, 'Europe/London' is the timezone being set.
3. Setting the timezone in the php.ini file:
You can also set the default timezone directly in the php.ini
configuration file, which is the main configuration file for PHP. Find the line that says ;date.timezone =
, uncomment it by removing the semicolon at the start, and set it to your desired timezone:
date.timezone = "Asia/Kolkata"
After you make this change, you'll need to restart your web server for the changes to take effect. Note that this will set the default timezone for all PHP scripts, not just a single script.
PHP date_default_timezone_set()
example:
date_default_timezone_set()
.// File: set_timezone.php date_default_timezone_set('America/New_York'); // Perform operations with adjusted timezone
List of supported timezones in PHP:
// File: list_timezones.php $timezones = timezone_identifiers_list(); // Output the list of timezones print_r($timezones);
Set default timezone in PHP globally:
// File: php.ini date.timezone = "America/New_York"
PHP set timezone for date and time functions:
// File: specific_timezone.php $date = new DateTime('now', new DateTimeZone('Europe/London')); // Perform operations with the specified timezone
How to convert timezones in PHP:
DateTime
and DateTimeZone
objects to convert between different timezones.// File: convert_timezone.php $date = new DateTime('2023-01-01 12:00:00', new DateTimeZone('America/New_York')); $date->setTimezone(new DateTimeZone('Europe/London')); // Perform operations with the converted timezone
PHP timezone offset calculation:
// File: timezone_offset.php $timezone = new DateTimeZone('America/New_York'); $offset = $timezone->getOffset(new DateTime('now')); echo "Timezone offset: " . ($offset / 3600) . " hours";
Set timezone in PHP.ini file:
php.ini
file to set the default timezone for the entire PHP environment.; File: php.ini date.timezone = "America/New_York"