All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Date Default Timezone Set PHP India

Last Updated : Jul 1, 2023

Date Default Timezone Set PHP India

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.

$timezone = date_default_timezone_get();
echo $timezone;
echo "<br>".date('d-m-Y H:i:s');
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. 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.
  3. Here we defined date_default_timezone_set() method with ‘Asia/Kolkata’ value now it will changed to india timezone.
  4. 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.
  5. Whether it prints result same as we specified then we sets time zone successfully to india otherwise it failed.
  6. 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.
  7. 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.
  8. 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.

Conclusion :-

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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪