How to Set the Timezone in PHP

If you notice the incorrect time in your PHP scripts, the likely culprit is that the hosting server is in a different timezone.

For example, when you’re an InMotion PHP web hosting customer, you can select from data centers in Washington, D.C., or California, which means the server’s timezone will be either Eastern Standard Time (EST) or Pacific Standard Time (PST).

If you want to change that, it’s a quick fix. Simply insert a line of code into your php.ini file and the location you set in the code will determine the time to display in your PHP scripts. Keep reading to learn how.

Set the Timezone in PHP

  1. Open your php.ini file with the File Manager in cPanel .
  2. Add the following line of code to top of your php.ini file: date.timezone = "US/Central".

    Timezone in the php.ini file
  3. Replace US/Central with the timezone from here that corresponds to the time you want to display.
  4. Once you have entered the desired timezone, click Save Changes to save the file.

    Save changes for new time zone
  5. Now, you can check your phpinfo.php page to verify the change took place.
Timezone setting before changeTimezone after change
Drag the arrow to the right to see the change in the timezone setting

Congratulations! You are now able to set the timezone in PHP. If you wish to see more guides with tips and information for your website issues, please see our InMotion Hosting Support Center!

Sell hosting using your own brand with a powerful Reseller Hosting plan, now with more resources and faster storage using NVMe SSDs at no extra cost.
AC
Arnel Custodio Content Writer I

As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.

More Articles by Arnel

38 thoughts on “How to Set the Timezone in PHP

  1. Painful lesson for those application folks who three weeks ago say that they went though their testing (regressive?) and they come to you and tell you things are on fire. I guess as person that upgraded it, I should have checked it myself.

  2. Dear Members

    I have changed the timezone ini.php file and its changed there successfully even i tested with phpinfo.php it showing there same as per my chnages. but the time on my website still same as before (wrong time). So, please advice what to do? 

     

    Thanks

    Shoev

    1. We are happy to help you troubleshoot further but will need some additional information. How/where is the time displaying on your website? Can you provide a link to the site for us to see the time?

      Thank you,
      John-Paul

  3. I set timezone as asia/kolkata in my server but it showing 4 minutes ahead than actual timezone time,plz help mee.

    1. Hello Radha,

      If you made the change in the php.ini file then it’s possible that you have another PHP.INI file where the change is not taking precedent. Make sure that the php.ini file with the time zone setting is recursive. Here’s an article on how to do this: How to make my php.ini file recursive. If the problem continues, then please let us know.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  4. What you are specifically looking for in this case for West Africa, and would be the same no matter where in the world you are is to find out first what your UTC offset is and then use one in the list that matches that.  For example, I live in the Pacific Northwest in the USA and our offset is -7 (that is minus seven) and at present we are in Daylight Savings time (aka PDT). When not in DST time of year we are -8 PST.

    So we are minus seven hours from GMT aka UTC which is at 0 and sometimes expressed +0.

    Short answer is find your offset and use the one that matches.  There are many and I think some with the same UTC offset.

    1. There is no php.ini in any accounts in my reseller account – all sites that check date get the php warning about timezone. The .htaccess fix does not work. Do I need to request a php.ini file created every single time I create a site? This seems excessive. It seems leaving warnings on as a default is a problem.

    2. On my VPS, phpinfo() says user_ini.filename .user.ini

      So instead of php.ini, I had to create .user.ini in the cPanel’s /public_html/ directory for the date.timezone setting to take effect on my site.

  5. I found this helpful.

    When no default is set, it goes to UTC.  This is what I found with one server.

    For another it was set to Germany/Berlin !

    I updated php.ini, but phpinfo() did not update even when pressing F5,  until I recycled apache.  Then I went into the app and the times were correctly displayed.

  6. I have set date.timezone = “America/New_York” in my php.ini and set it recursive in my .htaccess.. Still getting Los Angeles on my phpinfo page.

    1. Hello Larry,

      Thank you for contacting us. Did you add the setting to the top of your php.ini file?

      Can you provide a link to your site for us to test?

      Thank you,
      John-Paul

  7. I would like to know how to go back automactically to home page 3 seconds after submitting the data. I’d tried with header but it goes back and forth. Please guide me through that. Thanks a lot for your time.

    1. Hello Andy,

      Unfortunately, providing programming support is beyond the scope of our support. Please consult with a developer or forum that helps with programming a solution to meet your needs.

      Regards,
      Arnel C.

  8. I just moved my accounts to IM and am testing.  I went to a Members Only login page and got two errors re: php that I am not getting on the old server.  I checked the version of php and set it to the same 5.3 that it was using before. 

    Warning: session_start() [function.session-start]: It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘America/Los_Angeles’ for ‘PDT/-7.0/DST’ instead in /home/uscold/public_html/mo/membersOnly.php on line 6

    Warning: session_start() [function.session-start]: open(/tmp/sess_e079720753ff740ed0a801cea301a3d3, O_RDWR) failed: Permission denied (13) in /home/uscold/public_html/mo/membersOnly.php on line 6

     

    I tried adding the line in php.ini for the timezone as instructed on this page but am still getting these errors.  Can you point me to a solution?  Thanks.  (I’m not knowledgeable in php beyond very simple basics)

  9. If the times that the data is logged in your DB are according to GMT (example being) then you can do a fairly simple convert according to what UTC offset by adding or subtracting an hour (eg. 3600 seconds per hour if logged using a Unix Timestamp).

    As for further entries in the DB, I would recommend to use the same method as has been used to this point unless an upgrade is in order.  In which case, change relevant data points to match as you desire and and for all previous data, use a conversion script.

    Some simple logic can be used for this test to create a converter.

    if($db_time < $new_stamp) {

       // add the lines as needed depending of the UTC offset

    }

  10. I did tried it and it had changed in my php.ini file but whenever I submit the data to my database the time still wasn’t changed in my database. thanks a lot for your time.

  11. If you are using any form of PHP templating, this should still work if placed in the top of your template page (or in the top of each page that needs this): ini_set(“date.timezone”, “America/Los_Angeles”);

    This will make sure it stays as your script needs it and it is just a change for your script(s) only.

    1. Hello Andy,

      Thank you for contacting us. The above guide explains just that. Were you able to follow the above guide?

      Did you have trouble with a certain step?

      Thank you,
      John-Paul

  12. This would be another way to use / set this >

    <?php

    if(ini_get("date.timezone")!=="America/Los_Angeles") {

    if(ini_set("date.timezone", "America/Los_Angeles")===FALSE) {

    echo "Unable to set timezone."; // You could send to another var for display elsewhere too.

    }

    }

    ?>

     

    That is if you where using a PHP templating method by which you used the same template page across your entire site.  This would go right near the top to be sure to check the setting and change it to the timezone desired.

     

    This page may be of help to anyone in the Americas too > https://php.net/manual/en/timezones.america.php

    1. Hello aravindhan.m,

      Thank you for your suggestion. I will review this article and determine if a video would help people set the timezone for PHP in the php.ini.

      If you have any further questions, feel free to post them below.
      Thank you,

      -John-Paul

    2. Hello Silvi,

      Thank you for your question. Since I could not locate your account with us, here is some background information on the php.ini file. This is from our guide on How to update your local php settings.

      Only users running on a server with PHP loaded as a “CGI module” will have a php.ini file. The php.ini file by default will be located at public_html/php.ini. If you do not see a file named php.ini in your public_html, please contact our Support Department and request that we create one for you.

      If you have any further questions, feel free to post them below.
      Thank you,

      -John-Paul

  13. Be sure after you have done this to stop apache and restart apache… Well shut down the web server and restart it again so php will pick up the timezone change.

    1. Hello Anthony,

      Thank you for your comment. But, it is not necessary to restart the server when changing the timezone.

      You will also not have the ability to do so in a shared servers setting, since it will bring everyone’s site down.

      If you have any further questions, feel free to post them below.
      Thank you,

      -John-Paul

Was this article helpful? Join the conversation!