Server Time-Zone To Local Time-Zone

Discussion in 'Tutorials' started by rockee, Apr 5, 2008.

  1. rockee

    rockee New Member

    Messages:
    120
    Likes Received:
    0
    Trophy Points:
    0
    This subject has been a migraine issue for many in a shared hosting environment as it used to have to be resolved by many support request to the server's administrator to have it changed at the server's configuration file access level.

    Some shared hosting providers refused this request either because they were inept, too busy or they just simply could not be bothered.

    Well on X10 Hosting by creating or editing a .htaccess file, this time change can be performed very simply by just about anyone who needs to set the server's time zone to your local time zone - mainly for the correct time being returned in forms and perhaps in forums etc.
    (You will know when this change, from the server's time zone to your local time zone, or indeed any time zone of your choosing, is required).


    EXISTING .HTACCESS FILE

    1. If there is a .htaccess file already in the public_html folder it is very important to make a backup/copy, just in case you make a typo and lose access to your site via a web browser - usually a Server 500 error.
    (this is the only indication of anything wrong with your editing, plus of course, your error log in cPanel will point out any mistakes you might have made).


    2. For an overall change that will affect all of your hosting account, open the .htaccess file located in your public_html folder for editing.

    The .htaccess file in your web root folder - public_html - can be edited via your cPanel File Manager(Legacy) or by using your favourite ftp client (quicker and with more control).


    3. Add this line to the very top of the .htaccess file, before any other entry and follow the entry with a carriage return:

    SetEnv TZ Australia/Brisbane


    I know this link is a php site but your Apache server uses these designated time zones, so you can look up your specific TZ here:
    http://www.php.net/manual/en/timezones.php


    4. After editing, save the file (see below if editing on your PC) and try out your new TZ in your particular web site application.
    Job done!




    CREATING A .HTACCESS FILE

    1. To create a .htaccess file is just the same as creating any file or folder in the cPanel File Manager(Legacy) and in an ftp client.


    2. In a Windows OS the .htaccess can produce errors when trying to create and save this file as .htaccess - just create the file by adding the SetEnv directive with your Time Zone added, as the first line, using your preferred TEXT editor (not a word processor) and save it as htaccess.txt


    3. Upload the htaccess.txt file to your designated folder in your X10 Hosting account.


    4. Now rename the file from htaccess.txt to .htaccess - test the TZ change in your web application.
    Job done!




    USING PHP

    You can dispense with the .htaccess approach if you feel you are not confident to edit this file or you don't wish to use this approach, but instead you have an index.php file that points to your php application, like a contact form, so the results posted to you via email have the local time the enquiry was made and not to have to try and struggle with time zone conversions into your local time zone.

    1. Open your index.php for editing or the php file containing the code that performs any time related tasks for example formmail.php, again in a TEXT editor only, as a Windows word processor will not save in the correct format for an Apache server even when you select from the File menu so to do.


    2. Add this as the very first line (no spaces above), the very top of the page above everything else including the W3C Document declaration:
    Code:
    <?php
    date_default_timezone_set('Australia/Brisbane');
    ?>
    Again use this link below to get your own local time zone and replace only the Australia/Brisbane part - leave everything else as is:
    http://www.php.net/manual/en/timezones.php


    3. Save the file and upload, if required, to your X10 Hosting account folder and test the time zone change in your web application.
    Job done!

    NOTE - If your file/page has mixed php and html code then the file must have a .php file extension or else the server will not parse the php code but simply read it as text and display the php code as text in your browser when you call that page using the URI/URL in your browser's address bar.


    Good luck with it all and I sincerely hope it works for you as it has for so many migraine sufferers in the past. :)



    Also you might like to read this thread:
    mastrodonte113
    if you have FrontPage Extensions installed in your hosting space, also if you want to see the results of this .htaccess edit from a forum member who performed this time-zone change to good effect after months of wrangling with this TZ issue:

    This post, in particular, for the details of the TZ changing provided for mastrodonte113 and for an explanation of what the .htaccess actually does in relation to TZ changing and how the changes using a .htaccess file can be on a folder by folder basis, which, if you have sub-domains, will allow you to change the individual sub-domains' TZ - very handy:
    Rockee's reply


    Here is a link to a related topic that maybe of use to some of our members:
    Date and Time Related Conversions/Calculators


    BTW, what time is it now, please? :)

    Regards,
    Rocky
     
    Last edited: Apr 6, 2008
  2. tittat

    tittat New Member

    Messages:
    2,479
    Likes Received:
    1
    Trophy Points:
    0
    oh! it helped a lot.Thanks this was what i was searching for the past few weeks.
    ****Reputation given******
     
  3. cursedpsp

    cursedpsp New Member

    Messages:
    237
    Likes Received:
    0
    Trophy Points:
    0
    Another way without modifying you .htaccess file is quite simple

    place this bit of code on every webpage
    Code:
    putenv("TZ=UK");
    
    thats if you live in the UK, if you were in the US/EAST for example
    Code:
    putenv("TZ=US/Eastern");
    
    or you can set a timeformat
    Code:
    putenv("TZ=GMT");
    
    Great tut by the way ;)
     
    Last edited: May 10, 2008
  4. rockee

    rockee New Member

    Messages:
    120
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the additional code snippet which is also covered from this php manual link and those wishing to use this code snippet should read the manual for possible security issues and the rest of the putenv Description so they understand what the implications are.

    Also read the User Contributed Notes for users experiences in the above manual link.

    Remember if you add the above code in every web page you need to add <?php></?> and save the web page file with a .php extension not a .html extension so the server knows to parse the php code.

    Example:
    Code:
    <?php>
    putenv("TZ=UK");
    </?>
    Regards,
    Rocky
     
  5. mattura

    mattura Member

    Messages:
    570
    Likes Received:
    2
    Trophy Points:
    18
    Yet another method - place this code at the top of all pages (an include would be a good idea):
    PHP:
    date_default_timezone_set('GB');  //set British timezone
    This one replaces calls to date()/mktime() and other timestamps to the local date/time.
     

Share This Page