Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • Google+
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Web Servers ›
Setting a Timezone in PHP

Setting a Timezone in PHP

August 29, 2010 Web Servers Jesin A 1 Comment

Using the javascript date/time function is easy because its a client-side language so it displays your system time. But its not the same case with PHP. Since its a server-side language it displays the server’s time which can be irritating because you might have your server located in another country and you might live somewhere else. But changing the timezone is just a few clicks away and whats more, you can even set different timezones for different pages/directories.

Method 1:- Edit the php.ini file

Before editing the php.ini file please take a backup of it. Open the php.ini file using a text editor and navigate to the following line.

date.timezone =

After the equal to (=) symbol enter the desired timezone. See list of PHP timezones for a complete list of supported timezones. For example

date.timezone = America/Mexico_City

save the php.ini file and restart your web server. Create a PHP file and enter the following code

<?php print date("d/m/yy")."<br>".date("H:i:s"); ?>

save the file and access it via your web browser you should see the date and time of the timezone you entered. This change is done system wide so all web pages will reflect this change.

Method 2:- Create/Edit the .htaccess file

This method is for people using shared web hosting services. Others can also use this method for setting different timezones for different directories. Example, for the main directory you may have the Mexico timezone and for the directory /images you can have the Australian timezone. Edit the .htaccess file, if its not there create a file named .htaccess inside the www or htdocs or public_html folder. Add the following line to it

php_value date.timezone [timezone name]

example,

php_value date.timezone America/Mexico_City

save the .htaccess file and do as mentioned in method 1 to test the configuration.

Method 3:- Use the ini_set() function

The ini_set() is a very useful function which allows an user to set specific options for a single page/file. This method can be used be anyone who want to apply different timezone settings to each of the PHP script.

Syntax:- ini_set("date.timezone","[timezone name]");

Example:- ini_set("date.timezone","America/Mexico_City");

Add this function to the beginning of every PHP script and you’re done. So you can have each script show a different time/date associated with a different timezone.

Related posts:

php category thumbnailHow to use custom php.ini files on shared hosting php category thumbnailPHP error_log .htaccess Default ThumbnailDisabling PHP Display Errors Default ThumbnailChange the default Directory Index in Apache Server Default ThumbnailRunning PHP code in HTML files

Tags: htaccess, php, php timezone

Comments

  1. megadr01d says

    October 10, 2013 at 4:55 am

    Old post but method 3 can also be

    date_default_timezone_set("[timezone name]");
    

    kthxbye

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.