/    Sign up×
Community /Pin to ProfileBookmark

Correct time when living in Europe?

Hi!

I use $visitor_time = date(“H:i:s”); to get the correct time, but the time on the server is one 1 hour back. What is the best way to add one hour?

Thanks!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscDec 01.2009 — $visitor_time = date("H:i:s",time()+3600)

OR, if your visitors will always be coming from the same timezone, then you can use

date_default_timezone_set(timezone_identifier)

and then just call:

date("H:i:s");
Copy linkTweet thisAlerts:
@MindzaiDec 01.2009 — date_default_timezone_set() is my preferred solution too. If you have a user system you can also let them choose a timezone, pass it to this function and have dates displayed properly without touching the body of your code.
Copy linkTweet thisAlerts:
@pkngauthorDec 01.2009 — Thanks!

Hmmm, but wouldn't the server time return the same time if the visitor browser request comes from the US, Australia or Europe? A little bit confused?!
Copy linkTweet thisAlerts:
@aj_nscDec 01.2009 — Yes, exactly. I was going to say that in my first post, but I thought I'd just leave it simple.

For this you would have to detect what timezone the user is in (http://www.ipinfodb.com/index.php)

and then, I would, use a session variable to store the timezone and everytime a script is called then use

date_default_timezone_set($session_var_w_timezone_identifier)

and then you can still use the usual

date('H:i:s')

(the situation complicates itself further if you are storing timestamps in MySQL)

EDIT: ipinfodb returns an hour offset, not an identifier. So you would have to use a function to get an identifier from the offset.....OR you could set a session variable that contains the offset returned from ipinfodb and use

date('H:i:s',time()+(60*60*$session_var_w_timezone_offset));

provided you always want to display the CURRENT TIME
×

Success!

Help @pkng spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...