Can PHP do this

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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!

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
if($myOffset >= 0)
    $myOffset = "+" . $myOffset;

$theTime = time();
//make localtime into GMT + something else:
$theTime = strtotime($myOffset . " 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():

Re: Can PHP do this

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

Re: Can PHP do this

Quoted text here. Click to load it

See gmdate in the manual

Quoted text here. Click to load it

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:
echo "GMT  : ".gmdate('r',$now)."\n";
echo "Local: ".date('r',$now)."\n";
echo "Tokyo: ".date('r',$now)."\n";

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)

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 :)

Site Timeline