Can PHP do this

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

    Can PHP do this

    Can PHP get the current GMT time and add an hour to it? for example i think
    Japan is GMT +8 so would it be possible to write a PHP script to show the
    current time in Japan based on that?
    Thanks, im new to PHP but it seems like an excellent language compared to
    everything else ive tried!


  • ZeldorBlat

    #2
    Re: Can PHP do this

    $myOffset = date("0"); //how many hours off GMT I am (i.e. +0200)

    //flip the sign and make it hours (i.e. -2)
    $myOffset = -1 * ($myOffset / 100);

    //For example, we want to add 5 hours to GMT:
    $myOffset += 5;

    //If offset is positive, need the plus sign (in the string) for
    strtotime()
    if($myOffset >= 0)
    $myOffset = "+" . $myOffset;

    //localtime:
    $theTime = time();
    //make localtime into GMT + something else:
    $theTime = strtotime($myOf fset . " hours", $theTime);

    Now $theTime contains a UNIX timestamp of GMT plus some number of hours
    (5 in this case).

    To make $theTime be a little more readable, check out date():



    Comment

    • Dave Turner

      #3
      Re: Can PHP do this

      excellent!! many thanks kind sir :-)
      Reading that manual reference now


      Comment

      • Dave Turner

        #4
        Re: Can PHP do this

        this one line seems to be working beautifully, do you think its ok to use?

        echo gmdate ("h:ia, F j Y", time() + (4 * 3600));

        (where the 4 means GMT +4)


        Comment

        • ZeldorBlat

          #5
          Re: Can PHP do this

          Oops, yeah, you're right. I thought there was something out there
          already that did it, but I'm a little hungover and couldn't find it :)

          Comment

          • Daniel Tryba

            #6
            Re: Can PHP do this

            Dave Turner <nobody@nowhere .nohow> wrote:[color=blue]
            > Can PHP get the current GMT time and add an hour to it? for example i think[/color]

            See gmdate in the manual
            [color=blue]
            > Japan is GMT +8 so would it be possible to write a PHP script to show the
            > current time in Japan based on that?[/color]

            Japan is at +9 at this moment in time (but that can change in the future
            (or past)). Don't count on absolute offsets.

            It's has been asked before (many times):

            $ php4 /tmp/tz.php
            GMT : Thu, 07 Apr 2005 19:17:14 +0000
            Local: Thu, 07 Apr 2005 21:17:14 +0200
            Tokyo: Fri, 08 Apr 2005 04:17:14 +0900

            The source:
            <?php
            $now=time();
            echo "GMT : ".gmdate('r',$n ow)."\n";
            echo "Local: ".date('r',$now )."\n";
            putenv("TZ=Asia/Tokyo");
            echo "Tokyo: ".date('r',$now )."\n";
            ?>

            Comment

            Working...