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

8

8

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?

Gneiss answered 2/5, 2012 at 14:43 Comment(1)
Use Javascript in the front end to take the server timestamp, and calculate and print the local time. It'll be easier if the server timestamps you are storing are UTC+0.Paulo
B
9

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/

Bilek answered 2/5, 2012 at 14:50 Comment(2)
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.Jimenez
https://mcmap.net/q/472309/-how-to-display-localized-time-based-on-a-visitor-39-s-location this may help in showing php now time in visitor's time zoneFerryman
F
1

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).

Fillian answered 2/5, 2012 at 14:53 Comment(1)
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.Jimenez
L
1

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.

Lesbos answered 2/5, 2012 at 14:57 Comment(0)
A
0

I don't have the insight as to how to do this in PHP, but since you're asking about javascript as well, you can use the Date object.

var d = new Date();  // creates a Date object and initializes it with
                     // user's current time

d.getTimezoneOffset();  // time zone offset in minutes

d.getDay();
d.getHours();

/* etc... gets information about the user's local time */

For full reference: http://www.w3schools.com/jsref/jsref_obj_date.asp

Note also that this is, of course, based on user's computer time settings. It doesn't check the user's worldwide position based on his network address. If you actually want to do that, then disregard my answer :)

Acquire answered 2/5, 2012 at 14:52 Comment(1)
I need the same thing, but to be calculated on the base of the timestamp, returned by the server.Jimenez
B
0

As other answers have said, Javascript is the right technology for this, however if you insist on using php, then one possible way you can do it is by using a geolocation library or service to locate guess the timezone from the IP Address.

This will however always be a best guess solution and in many cases the ip address a visitor is coming from, may not agree with the timezone the user is currently in.

Buckler answered 2/5, 2012 at 14:54 Comment(2)
I'm ready to use javascript if I have to. As you may guess I don't want to rely on geolocation tracking.Jimenez
@Buckler 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?Saunder
F
0

if someone wants to show PHP time in visitor's System time zone

<?php
$now = new DateTime();
$date_created=$now->format('Y-m-d H:i:s');
$contents = htmlentities("<script>var x=new Date('$date_created' +'+0000');document.write(x.getMonth() + '-' + x.getDate() + '-' + x.getFullYear()+ ' ' + x.getHours() + ':' + x.getMinutes());</script>");
echo html_entity_decode($contents);
?>

this will show the server time in visitor's time zone.If you set the server time to UTC +00.this javascript function can be used if someone want in format change

  function detectAmPm(hours, min) {
        let time;
        if (hours >= 12) {
            time = "PM";
            hours = hours - 12
        } else {
            time = "AM";
        }

        if (hours < 10) {
            hours = "0" + hours;
        }
        if (min < 10) {
            min = "0" + min;
        }

        return {min: min, hours: hours, time: time};
    }
Ferryman answered 12/6, 2020 at 12:23 Comment(0)
L
0

The easiest way is to get the browser to tell your script what the user time and zone is. For example:-

<input name=tz type-hidden id=tz><script>document.getElementById("tz").value=new Date;</script>

Then, your PHP will get their current time and zone next time they send anything to your chat: e.g.

'Wed Nov 03 2021 09:21:01 GMT+1000 (Australian Eastern Standard Time)'

You can then use the GMT+xxx code to convert your server time to their local time in PHP.

Letsou answered 2/11, 2021 at 23:22 Comment(0)
F
-2

PHP is an server-side scripting language therefore you cannot get local time of the visitor unless you use GeoIP. You should use JavaScript instead.

Fico answered 2/5, 2012 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.