3

I get the ip address of the user by function.

e.g

$ip_address = getIP();

Now I want display current localized time and date based on a visitor's Ip address using PHP. if it is possible than guide me How?

Waiting for the Help and Thanks In advance.

The One
  • 33
  • 2

1 Answers1

1

As pointed out in comments - this has been asked and answered

But one answer not included is to use a Javascript callback, or store tiem offset in a cookie from Javascript (or adjust times on display).

Example using a callback:

  • use a session in PHP (session_start, or create your own session handler)
  • either on the first page (where you don't have dates) or send a blank holding page that sets the session and use Javascript to redirect to the real page with appending the timezone difference

Code:

var d = new Date()
window.location.href = PageYouWant + '?offset=' + d.getTimezoneOffset();

(Vary according to your needs)

  • then on receiving the call back, remember the offset against the session. Then send a 303 redirect to the URL without the offset in the URL and you have the data you need.

You can do a similar thing with cookies (if you don't have the cookie, send a holding page that creates the cookie and calls you back).

Keep the holding page blank and no-one will notice it - just looks like the page is loading.

Robbie
  • 17,605
  • 4
  • 35
  • 72