Home » Php » php – How to display localized time, based on a visitor's location?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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/

Answer:

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

Answer:

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 🙂

Answer:

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.

Answer:

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.

Answer:

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};
    }

Answer:

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.