By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Time help

P: n/a
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

Jul 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I suggest you read the php manual entry for the date() command
http://php.net/date

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:
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
Jul 7 '06 #2

P: n/a
NC
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

Jul 7 '06 #3

P: n/a
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_timezone_get();
// set the new timezone
date_default_timezone_set('America/New York');
// display the time and timezone
echo date('H:i:s T');

-david-

Jul 7 '06 #4

P: n/a
*** 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_offset) . "\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_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
--
Jul 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.