1

Want to ask. How do I create a website that is able to show different time when at different country. Example:

If user is using the website at Japan, it will show Japan's time.

While If user is using the website at Britain, it will show Britain's time.

Right now I am using this code:

<?php
   date_default_timezone_set("Asia/Tokyo");
   echo "Today's date is :"; 
   $today = date("d/m/Y"); 
   echo $today;
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ng Wei Shen
  • 75
  • 11

2 Answers2

1

You should do it on the client-side. I have read a forum once and they recommended it to be done in the client per se. Take what you think might help you from this javascript code example:

var now = new Date();
var utcString = now.toISOString().substring(0, 19);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var localDatetime = year + "-" +
    (month < 10 ? "0" + month.toString() : month) + "-" +
    (day < 10 ? "0" + day.toString() : day) + "T" +
    (hour < 10 ? "0" + hour.toString() : hour) + ":" +
    (minute < 10 ? "0" + minute.toString() : minute) +
    utcString.substring(16, 19);
//var datetimeField = document.getElementById("myDatetimeField");
//datetimeField.value = localDatetime;
alert(localDatetime);
Frank Lexer
  • 310
  • 1
  • 11
1

There is no function in php that can get user based timezone. But there is way around. First you have to grab user IP address, then you have make a call to a third party service to get geo information. Thus you can get user timezone.

The following function is fool-proof solution to get user IP using php so far. And credits goes to https://stackoverflow.com/a/38852532/7935051

<?php

function getClientIp() {
    $ipAddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipAddress = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipAddress = $_SERVER['HTTP_X_FORWARDED'];
    } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
        $ipAddress = $_SERVER['HTTP_FORWARDED'];
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ipAddress = $_SERVER['REMOTE_ADDR'];
    } elseif (getenv('REMOTE_ADDR')) {
        $ipAddress = getenv('REMOTE_ADDR');
    } else {
        $ipAddress = 'Unknown';
    }

    return $ipAddress;
}

Now you have to depend on the third party services to get geo information. This is very true for this type of jobs. There are several free services on internet, for example, geoPlugin. You may use it. See more details.

// Gets the client IP
$userIp = getClientIp();

$geoInfo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip={$userIp}"));

// Gets the timezone
$timezone = $geoInfo['geoplugin_timezone'];

// Sets user timezone, otherwise uses default
if ($timezone) {
    date_default_timezone_set($timezone);
} else {
    date_default_timezone_set('Asia/Tokyo');
}

echo "Today's date is :";
$today = date("d/m/Y");
echo $today;

BTW you may debug $geoInfo variables to get more information

unclexo
  • 3,691
  • 2
  • 18
  • 26