Setup Default Timezone in PHP

  • iftekhar
  • Jul 13, 2016
  • 0 comment
  • 1026 Views
  • Print

If you are developing a PHP based website, or working on your custom PHP driven CMS that will interact with MySQL or any other database chances are there that you may need to define default timezone for your site or application. Specially, if you are using date/time function on your site.

If the timezone of your site is not defined for your site or application, PHP date / time function will not display your desired result. To avoid such issues, you can define the timezone at the very beginning of your script so that date or time function can adjust themselves based on your desired timezone and acts accordingly. Here is what you can write on your script.

<?php
date_default_timezone_set('America/New_York');
?>

I am using “America/New_York” on my application, however if you are living any other part of the world, feel free to check out the available timezones from the link provided below.

Once you set-up your default timezone, there should not be any issue with your date/time function. Case in point, if you are using WordPress on your site, you can find that WordPress has the option (Settings -> General) to setup default timezone that your site can follow. The timezone data gets stored on your database (wp_options table, prefix may vary) which can be retrieved and used on numerous native WordPress function. I hope you get the idea.

Now, if you are already using any PHP driven application or site and not quite sure which timezone your site is currently using, simply write following snippet on a page of your application and you should find the name of your timezone your site is currently following.

<?php
echo date_default_timezone_get();
?>

I hope you would find this post useful. Thanks.

References: date_default_timezone_set, date_default_timezone_get, timezones

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.