How to display localized time, based on a visitor's location?

10,146

Solution 1

You can make a educated guess on the users timezone with geo IP

http://php.net/manual/en/book.geoip.php

Or you can use javascript:

http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/

Solution 2

You can accomplish this with PHP if you know the users IP-address ($_SERVER['REMOTE_ADDR']). Look into Geo-IP (http://php.net/manual/en/book.geoip.php) for matching location<->ip-address

The IP-address does not neccesary reflect the users "real" position if (for instance) he/she is behind a proxy. I've also seen some strange behaviour when using mobile devices (GPRS/edge/3G/HSDPA).

Solution 3

The only guaranteed way to display exactly the time set up on user PC is to use JavaScript. You can try server-side geo detection, but databases are never perfect or user well could be a remote client of some provider from absolutely different time zone. Send your timestamps as seconds from Unix epoch time, then initialize Date object in JavaScript with this time and use its .get (without UTC) methods to retrieve corresponding pieces of time in user's local representation. Providing user with option to use some specific timezone in case he wants to override local time for some reason is a good idea as well.

Share:
10,146
Дамян Станчев
Author by

Дамян Станчев

Developer, student and even more :)

Updated on June 11, 2022

Comments

  • Дамян Станчев
    Дамян Станчев almost 2 years

    We're developing a website, that will be used from people all over the world. There's a chat section in the website and we want the messages to appear with a timestamp. I'm storing in a database a timestamp for each message.

    What is the way to show the right time for each message to visitor, based on his local time.

    I don't want to ask the user for his timezone. Is there a way to do this, using only PHP? If not - what is the way to show the current time, based on the visitors' current time, using javascript?

  • Дамян Станчев
    Дамян Станчев about 12 years
    I'm ready to use javascript if I have to. As you may guess I don't want to rely on geolocation tracking.
  • Дамян Станчев
    Дамян Станчев about 12 years
    I need the same thing, but to be calculated on the base of the timestamp, returned by the server.
  • Дамян Станчев
    Дамян Станчев about 12 years
    I'll skip the geolocation tracking - It's not accurate, requires a lots of space and time (in the database) if it's on my server and most of the API-s are paid. I'll look for a way to do this using javascript and a server timestamp.
  • Дамян Станчев
    Дамян Станчев about 12 years
    Here's what I used: var date = new Date(Date.UTC(year,month,day,hour,minute)); $('#'+selector).html ( (date.getHours()>9?date.getHours():('0'+date.getHours())) + ':' + (date.getMinutes()>9?date.getMinutes():('0'+date.getMinutes‌​())) ); where year,month,day,hour,minute are passed by server from the database.
  • Rana Nadeem
    Rana Nadeem almost 4 years
    stackoverflow.com/a/62344221/12910765 this may help in showing php now time in visitor's time zone
  • Chidimma Nworah
    Chidimma Nworah about 2 years
    @Anigel does this display unique dates according to each unique user's location? Take for instance a website, can the date be visible once a user visits that site, disregarding whether it's a first visit?