#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Php : Customize Local Time


    The problem ....

    Let's suppose ....

    The server ( host provider ) is in USA

    The website owner is in Greece

    The owner wants to has greek time displayed in the website.


    A simple solution .....

    STEP 1. Create a php file for example time.php


    STEP 2. Check what time is it in you server.

    Write ......

    PHP Code:
    <?php 

    echo date ' H : i '   ) ;

    ?>
    .... and upload time.php on the server
    * H = hours in 24 format
    * i = minutes


    STEP 3. Calculate the time difference between the server and the locaction you need . In my case the location I need is 6 hours later so....

    PHP Code:
    $time date 'H' ) + 
    STEP 4. Delete your previous script in time.php and write instead :

    PHP Code:
    <?php 

    $server 
    date 'H' ) ;
    $minutes date 'i' ) ;
    $time date 'H' ) + ;

    echo 
    $time : $minutes "

    ?>
    ...Our time will be server time + 6 . That means if for example server time is "14:00" , the script we 've just wrote will print "20:00" . But the problem is if for example server time is "22:00" our script will print "28:00"..
    That can't be time !


    STEP 5. Calculate (24 hours) - ( Yours Hours Difference ) = x

    In my case 24h-6h = 18h
    so my max time in +6 format should be 17:59

    So.....

    PHP Code:
    <?php 

    $server 
    date 'H' ) ;
    $minutes date 'i' ) ;
    $time date 'H' ) + ;
    $maxtime18 ;

    ?>
    STEP 6. Now we just have to write time cases
    PHP Code:
    if ($server $maxtime ) echo "$time:$minutes"
    if (
    $server == $maxtime ) echo "00:$minutes";
    if (
    $server == 19 ) echo "01:$minutes";
    if (
    $server == 20 ) echo "02:$minutes";
    if (
    $server == 21 ) echo "03:$minutes";
    if (
    $server == 22 ) echo "04:$minutes";
    if (
    $server == 23 ) echo "05:$minutes"

    Final Script ....

    PHP Code:
    <?php 

    $server 
    date 'H' ) ;
    $minutes date 'i' ) ;
    $time date 'H' ) + ;
    $maxtime18 ;


    if (
    $server $maxtime ) echo "$time:$minutes";
    if (
    $server == $maxtime ) echo "00:$minutes";
    if (
    $server == 19 ) echo "01:$minutes";
    if (
    $server == 20 ) echo "02:$minutes";
    if (
    $server == 21 ) echo "03:$minutes";
    if (
    $server == 22 ) echo "04:$minutes";
    if (
    $server == 23 ) echo "05:$minutes";

    ?>
    Last Notes ....

    This example presented the case in which the server is in USA ,and the time we need is Greek. I presented some steps for you to write yours programme. Note that, if for example the time you need is 2 hours earlier you can write (-) instead of (+) e.g $time = date ( 'H' ) - 2 ;


    You can see an online example at:
    wrss.eu/examples/localtime.php
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Denmark
    Posts
    4
    Rep Power
    0
    It would be easier to use time function:
    PHP Code:
    <?php
    echo date('H:i'time()+(6*60*60));
    ?>
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Both of these methods are far out of date. The DateTime Class is the proper way to handle this, because it handles timezones, rather than trying to do the math by hand.

    It's ironic that our Mary here chose tonight to post this incorrect advice, since 2 hours from right now, the time zones of the United States are all changing by an hour. If you followed Mary's advice this morning, your site would be wrong tomorrow.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo