Time help

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

Threaded View
I'm creating a script to include time.

Here is my code.

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.


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",time()+7200); //7200 seconds == 2 hours

Hoopster wrote:
Quoted text here. Click to load it

Re: Time help

Hoopster wrote:
Quoted text here. Click to load 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);


Re: Time help

Hoopster wrote:
Quoted text here. Click to load it

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.

// save the current timezone
$my_tz = date_default_timezone_get();
// set the new timezone
date_default_timezone_set('America/New York');
// display the time and timezone
echo date('H:i:s T');


Re: Time help

*** Hoopster escribió/wrote (Fri, 07 Jul 2006 05:39:09 GMT):
Quoted text here. Click to load 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_offset) . "\n";

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

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_timezone_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

Site Timeline