Date Default Timezone Set PHP India
Last Updated : Jul 1, 2023
In this article we will show you the solution of date default timezone set PHP India, here we are going to show you example for set timezone using date_default_timezone_set() method.
The date_default_timezone_set() method function sets the default timezone used by all date/time functions in the script. If none of the above succeed then default timezone will be UTC.
Step By Step Guide On Date Default Timezone Set PHP India :-
Here we defined date_default_timezone_set() method with ‘Asia/Kolkata’ value it refers india timezone so we sets timezone to india then we checking timezone after setting using date_default_timezone_get() method.
Now we can print date with specific structure using date object then it will prints india date with time on webpage.
<?php date_default_timezone_set('Asia/Kolkata'); $timezone = date_default_timezone_get(); echo $timezone; echo "<br>".date('d-m-Y H:i:s'); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. When we creating variables then we have to use ‘$’ symbol before variable name and syntax is ‘$variable_name or $variablename’. The first letter or character of variable name should be alphabetic otherwise it throws error.
- Here we defined date_default_timezone_set() method with ‘Asia/Kolkata’ value now it will changed to india timezone.
- We can also check that changes by using date_default_timezone_get() method it returns current timezone so we specifying this method on variable ‘$timezone’ then we printed ‘$timezone’ on webpage.
- Whether it prints result same as we specified then we sets time zone successfully to india otherwise it failed.
- As we know for print or collect date or time we have to use date object so we used date object with some specific format to display whole date time string.
- After changes on timezone when we prints date or time then that will be changed based on timezone what we have set. So here we gets correct date and time of india.
- For display result on webpage we used echo statement. As we know echo is a statement which is used to displays output. We can use this with or without parentheses like echo or echo() and it does not return any value then echo is faster than the print statement. Print also same as echo.
In conclusion now we are able to know how to set default time zone as india using php.
When work with php we need to create and process php files at server location and then we need to start the server before execute the program.
When we executing this program on browser it will prints result of ‘Asia/Kolkata, 06-09-2022 00:40:49’ on webpage.
Likewise we can also change that time zone to some other country then print date and time on webpage we gets correct date time string based on country specification in ‘date_default_timezone_set()’ method.
I hope this article on date default timezone set PHP India helps you and the steps and method mentioned above are easy to follow and implement.