Time help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hoopster

    #1

    Time help

    I'm creating a script to include time.


    Here is my code.

    <?php
    echo date("H:i:s");
    ?>


    However, my server is 2 hours difference from me. What is the exact
    command that I can use to offset it.


    Please provide me with an exact code change as I'm new at this.


    Thanks





  • Mike Knight

    #2
    Re: Time help

    I suggest you read the php manual entry for the date() command


    You can specify a timestamp for date() to use. You can use time() to
    return the current time stamp, and add or subtract to it to get to the
    right time zone. time() returns an integer which is referred to as a
    unix timestamp. The number is in seconds, so you can add/subtract
    seconds to change the time.

    echo date("H:i:s",ti me()+7200); //7200 seconds == 2 hours


    Hoopster wrote:
    I'm creating a script to include time.
    >
    >
    Here is my code.
    >
    <?php
    echo date("H:i:s");
    ?>
    >
    >
    However, my server is 2 hours difference from me. What is the exact
    command that I can use to offset it.
    >
    >
    Please provide me with an exact code change as I'm new at this.
    >
    >
    Thanks

    Comment

    • NC

      #3
      Re: Time help

      Hoopster wrote:
      >
      <?php
      echo date("H:i:s");
      ?>
      >
      However, my server is 2 hours difference from me. What is
      the exact command that I can use to offset it.
      Since you forgot to mention whether the server is two hours ahead or
      two hours behind, here are your options:

      echo date('H:i:s', time() + 2*60*60);
      echo date('H:i:s', time() - 2*60*60);

      Cheers,
      NC

      Comment

      • David Haynes

        #4
        Re: Time help

        Hoopster wrote:
        I'm creating a script to include time.
        >
        >
        Here is my code.
        >
        <?php
        echo date("H:i:s");
        ?>
        >
        >
        However, my server is 2 hours difference from me. What is the exact
        command that I can use to offset it.
        >
        >
        Please provide me with an exact code change as I'm new at this.
        >
        >
        Thanks
        As others have pointed out, you could just add 2 hours (in seconds) to
        the offset but what about daylight saving time?

        A better way is to set the timezone value.
        e.g.

        // save the current timezone
        $my_tz = date_default_ti mezone_get();
        // set the new timezone
        date_default_ti mezone_set('Ame rica/New York');
        // display the time and timezone
        echo date('H:i:s T');

        -david-

        Comment

        • Alvaro G. Vicario

          #5
          Re: Time help

          *** Hoopster escribió/wrote (Fri, 07 Jul 2006 05:39:09 GMT):
          However, my server is 2 hours difference from me. What is the exact
          command that I can use to offset it.
          Some unsorted ideas:

          <?

          echo "System: " . date('H:i:s (T)') . "\n";

          $tz_offset=2*60 *60; // 2 hours
          echo "Offset: " . date('H:i:s', time()+$tz_offs et) . "\n";

          putenv('TZ=US/Pacific');
          echo getenv('TZ') . ": " . date('H:i:s (T)') . "\n";

          putenv('TZ=CET' );
          echo getenv('TZ') . ": " . date('H:i:s (T)') . "\n";

          ?>

          In my computer it prints:

          System: 20:57:13 (Hora de verano romance)
          Offset: 22:57:13
          US/Pacific: 19:57:13 (Pac)
          CET: 18:57:13 (CET)

          The putenv() examples display incorrect values, probably because they're
          system-dependent and I was just guessing.



          Since version 5 PHP has a new time zone directive, but I haven't tried it:

          date.timezone string

          The default timezone used by all date/time functions if the TZ environment
          variable isn't set. The precedence order is described in the
          date_default_ti mezone_get() page.



          --
          -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
          ++ Mi sitio sobre programación web: http://bits.demogracia.com
          +- Mi web de humor con rayos UVA: http://www.demogracia.com
          --

          Comment

          Working...