Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time zone problem

Status
Not open for further replies.

peterv6

Programmer
Sep 10, 2005
70
US
I'm using the following PHP code to put a timestamp on my web page:

<?php $today = date("l F dS, Y h:i A T"); ?>

The problem is, the computer that's hosting my PHP server is the ISP's machine in the Central time zone, and I'm in the Eastern time zone.

Is there any way programatically that I can adjust this time so that it displays Eastern time correctly?
Thanks...

PETERV
Syracuse, NY &
Boston, MA
 
jet042, any chance you'd be willing to give me an example how to code this? I looked at the manual, and they don't give a specific example. (I'm an extreme novice at PHP, trying to learn it on my own.) If anyone can provide a working example of setting the date to Eastern time, I'd appreciate it.

date_default_timezone_set(America/New_York)

PETERV
Syracuse, NY &
Boston, MA
 
Your guess at it was almost perfect. The timezone name is a string, so it needs to be in quotes. I usually use single, but it doesn't matter. I also usually suppress any errors with the @ operator just in case. So it would look like this:

PHP:
@date_default_timezone_set('America/New_York');
 
jet042, thanks. Unfortunately, I just found out my ISP is using version PHP 4.3.2. Really up to date. Any ideas about v4? I'm going to search for a better isp this week.

PETERV
Syracuse, NY &
Boston, MA
 
I guess since you know the difference between the server and your location you can just adjust the timestamp that is getting fed into date(). Here's how you would do that:

PHP:
//Adjust time forward by one hour (CST to EST)
$dtm_adjustment = 60 * 60;
$adj_now = time() + $dtm_adjustment;

$today = date("l F dS, Y h:i A T", $adj_now);

I think that will work. I haven't tested it.
 
OUTSTANDING! Thank you so much! This is exactly what I need! Thank you so much for your patience with a "newbie"! One day, I hope I'll be able to do the same for another lost soul! :)

PETERV
Syracuse, NY &
Boston, MA
 
for a solution that is more adaptable to other time zones, you could try

Code:
ini_set('date.timezone', 'America/New_York');

this will work provided the environment variable TZ is not set (or is not empty)
 
@peterv6
You're welcome. Learning any new language takes time, but you definitely came to the right place to find answers to your questions. The regulars on these forums are very skilled and I make it a point to come here every day and read what kinds of solutions they are providing. I always learn something new.

@jpadie
Isn't the date.timezone ini directive only available after PHP 5.0 or did I miss something?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top