Default timezone asia in php

date_default_timezone_set

date_default_timezone_set() sets the default timezone used by all date/time functions.

Instead of using this function to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone.

Parameters

The timezone identifier, like UTC , Africa/Lagos , Asia/Hong_Kong , or Europe/Lisbon . The list of valid identifiers is available in the List of Supported Timezones.

Return Values

This function returns false if the timezoneId isn’t valid, or true otherwise.

Examples

Example #1 Getting the default timezone

if ( strcmp ( $script_tz , ini_get ( ‘date.timezone’ ))) echo ‘Script timezone differs from ini-set timezone.’ ;
> else echo ‘Script timezone and ini-set timezone match.’ ;
>
?>

Читайте также:  Example

See Also

User Contributed Notes

  • Date/Time Functions
    • checkdate
    • date_​add
    • date_​create_​from_​format
    • date_​create_​immutable_​from_​format
    • date_​create_​immutable
    • date_​create
    • date_​date_​set
    • date_​default_​timezone_​get
    • date_​default_​timezone_​set
    • date_​diff
    • date_​format
    • date_​get_​last_​errors
    • date_​interval_​create_​from_​date_​string
    • date_​interval_​format
    • date_​isodate_​set
    • date_​modify
    • date_​offset_​get
    • date_​parse_​from_​format
    • date_​parse
    • date_​sub
    • date_​sun_​info
    • date_​sunrise
    • date_​sunset
    • date_​time_​set
    • date_​timestamp_​get
    • date_​timestamp_​set
    • date_​timezone_​get
    • date_​timezone_​set
    • date
    • getdate
    • gettimeofday
    • gmdate
    • gmmktime
    • gmstrftime
    • idate
    • localtime
    • microtime
    • mktime
    • strftime
    • strptime
    • strtotime
    • time
    • timezone_​abbreviations_​list
    • timezone_​identifiers_​list
    • timezone_​location_​get
    • timezone_​name_​from_​abbr
    • timezone_​name_​get
    • timezone_​offset_​get
    • timezone_​open
    • timezone_​transitions_​get
    • timezone_​version_​get

    Источник

    How to set timezone with PHP easily (multiple methods)

    Short guide on how to set the timezone using PHP using multiple methods.

    PHP101.net - How to set timezone with PHP

    Quick solution

    The easiest way to set timezone with PHP is by the date_default_timezone_set() function. This function sets the default timezone used by all date/time functions in PHP. For example, to set the timezone to Asia/Hong_Kong :

    date_default_timezone_set('Asia/Hong_Kong');

    For a full list of supported timezones, check the official PHP.net Documentation timezone list. That is how to set timezone with PHP. For more methods and examples, please check the rest of the article.

    Set timezone with PHP functions

    Set timezone with PHP using date_default_timezone_set

    date_default_timezone_set(string $timezoneId): bool

    Where $timezoneId is the string of the timezone to be set. The function will return true if the timezone is valid, else it will return false. Tips: To get the current timezone that is set in PHP configuration, use date_default_timezone_get function:

    echo date_default_timezone_get(); // returns the configured timezone

    Set timezone with PHP using DateTime

    Another method to set timezone with PHP is by object-oriented way using the DateTime object and the DateTimeZone class. The major difference here is the timezone will be set only per created object. Therefore, this method can be used when you want to have multiple variables with multiple timezones, or if the timezone is interchangeable. For example, we want to have two DateTime object with different timezones:

    $dateHongKong = new DateTime('2023-01-01', new DateTimeZone('Asia/Hong_Kong')); echo $dateHongKong->format('Y-m-d H:i:sP'); // 2023-01-01 00:00:00+08:00 $dateJapan = new DateTime('2023-01-01', new DateTimeZone('Asia/Tokyo')); echo $dateJapan->format('Y-m-d H:i:sP'); // 2023-01-01 00:00:00+09:00

    Another example is a single DateTime object that can have its timezone updated with DateTime::setTimezone() :

    // Hong Kong timezone $date = new DateTime('2023-01-01', new DateTimeZone('Asia/Hong_Kong')); // Change to Japan timezone $date->setTimeZone(new DateTimeZone('Asia/Tokyo')); 

    Set timezone with PHP using ini_set

    We can also modify timezone configuration dynamically using ini_set function to modify the date.timezone configuration variable:

    ini_set('date.timezone', 'America/New_York');

    Set timezone using server-side configuration

    • In php.ini file, look for the [Date] section;
    • Uncomment the date.timezone configuration line by removing the ; sign at the beginning of the line;
    • Input the desired timezone after the = sign.
    . [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = Asia/Japan . 

    Remember to restart the web server after changing the setting in php.ini .

    Final thoughts

    These are the methods to set timezone with PHP using multiple methods. We hope these methods are helpful to you, and that they can be utilized in many use cases.

    You might also like

    References

    Источник

    Set timezone in PHP

    The timezone value is important for any website because the date and time values are displayed based on this value. The PHP script uses the timezone value of the web server by default. This timezone value can be changed by modifying the value of the date.timezone directive in the php.ini configuration file or by adding the entry for timezone value inside the .htaccess file or using several built-in functions. Different ways of setting the timezone value in PHP have been shown in this tutorial.

    Set the timezone Value by Modifying the php.ini File

    One of the easiest ways to set the default timezone is by modifying the date.timezone directive inside the php.ini file. Suppose you want to set the default timezone to ‘Asia/Dhaka’. Open the php.ini file and search for the location of the date.timezone directive. Modify the line by using the following line:

    Save the file and restart the web server to set the date and time of the server based on the modified timezone value.

    Set the timezone Value by Modifying the .htaccess File

    Modifying the .htaccess file is another way to set the default timezone value. Open the .htaccess file and add the following line to set the default timezone value to ‘Asia/Dhaka’:

    Save the file and restart the web server to set the date and time of the server based on the modified timezone value.

    Set the timezone Value by Using date_default_timezone_set() Function

    The date_default_timezone_set() is the built-in PHP function to set the timezone value. The output of all built-in functions of PHP related to the default timezone will be changed after changing the timezone value using the date_default_timezone_set() function. The syntax of this function is given below:

    Syntax

    This function has only one mandatory argument. This argument sets a particular timezone. It returns True if the valid timezone value is passed in the argument. Otherwise, it returns False. The date_default_timezone_get() function is used to read the current timezone value of the server. So, this function can be used to check the timezone is set properly after setting the new timezone by using the date_default_timezone_set() function.

    Example 1: Set the timezone by Using date_default_timezone_set() Function

    Create a PHP file with the following script to set the default timezone to ‘Asia/Dhaka’ using the date_default_timezone_set() function. The date_default_timezone_get() function has been used two times to print the timezone value before and after using date_default_timezone_set() function.

    // Print the current timezone
    echo «The current timezone is » . date_default_timezone_get ( ) . «
    » ;
    // Change the current timezone
    date_default_timezone_set ( ‘Asia/Dhaka’ ) ;
    // Print the changed the timezone
    echo «The current timezone is changed to » . date_default_timezone_get ( ) . «» ;

    The following output shows that the default timezone was UTC, and the timezone has changed to Asia/Dhaka after setting the new timezone:

    Set the timezone Value by Using ini_set() Function

    The ini_set() is a very useful function of PHP to modify any PHP directive by using a script without accessing the php.ini file. This tutorial discussed earlier that the ‘date.timezone’ directive required to modify to change the current timezone value. So, the ini_set() function can be used to change this directive value. The syntax of this function is given below:

    Syntax

    The function’s first argument takes the directive name, and the function’s second argument takes the value. It returns a string value on success and a False on failure.

    Example 2: Set the timezone by Using the ini_set() Function

    Create a PHP file with the following script that will set the default timezone to ‘America/Chicago’ by using the ini_set() function. The date_default_timezone_get() function has been used two times to print the timezone value before and after using ini_set() function.

    // Print the current timezone
    echo «The current timezone is » . date_default_timezone_get ( ) . «
    » ;
    // Change the current timezone
    ini_set ( ‘date.timezone’ , ‘America/Chicago’ ) ;
    // Print the changed timezone
    echo «The current timezone is changed to » . date_default_timezone_get ( ) . «» ;

    The following output shows that the default timezone was UTC, and the timezone has changed to ‘America/Chicago’ after setting the new timezone:

    Set the timezone Value by Using DateTimeZone Class

    Using DateTimeZone class is another way to change the default timezone value of the server. The uses of this class for changing the timezone have been shown in the following example:

    Example 3: Set the timezone by Using DateTimeZone Class

    Create a PHP file with the following script that will change the timezone value two times and print the current date and time based on the current timezone value. The display() function has been defined in the script to print the current timezone value and the current date and time based on the timezone. It has been called for the first time to show the output based on the default timezone, which is ‘UTC’. It has been called the second time to show the output based on the changed timezone, ‘Asia/Dhaka’. It has been called the third time to show the output based on the changed timezone, which is ‘Canada/Atlantic’.

    // Create a date object
    $date = new DateTime ( ) ;

    function display ( )
    {
    global $date ;
    $timezone = $date — > getTimezone ( ) ;
    // Print the current timezone and datetime based on timezone
    echo «The current timezone is » . $timezone — > getName ( ) . «
    » ;
    echo «The current date and time is » . $date — > format ( ‘d-M-Y h:i:s’ ) . «
    » ;

    // Call function to print the output based on default timezone
    display ( ) ;

    // Change the timezone to ‘Asia/Dhaka’
    $date — > setTimezone ( new DateTimeZone ( ‘Asia/Dhaka’ ) ) ;

    // Call function to print the output based on the changed timezone
    display ( ) ;

    // Change the timezone to ‘Canada/Atlantic’
    $date — > setTimezone ( new DateTimeZone ( ‘Canada/Atlantic’ ) ) ;

    // Call function to print the output based on the changed timezone
    display ( ) ;

    The following output will appear after executing the previous script:

    Conclusion

    Five different ways of changing the timezone value are shown in this tutorial. If the PHP user has no permission to change the php.ini or .htaccess file, then the user can use any of the built-in functions discussed in this tutorial to change the timezone value.

    About the author

    Fahmida Yesmin

    I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

    Источник

Оцените статью