How to Get the Exact Local Time of Client

How can I get the user's local time instead of the server's time?

As mentioned by everyone PHP only displays server side time.

For client side, you would need Javascript, something like the following should do the trick.

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

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

document.write("<b>" + hours + ":" + minutes + " " + "</b>");

And if you want the AM/PM suffix, something like the following should work:

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

var suffix = "AM";

if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}

if (hours == 0) {
hours = 12;
}

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

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");

Here is a list of additional JavaScript Date and Time functions you could mess around with.

Get time & date independent to the local time of client pc

If you do it with any server side language like php, it will display server time independent of client's computer. Only client side scripts will display client pc time

like php time()

get client time zone from browser

Look at this repository pageloom it is helpful

download jstz.min.js and add a function to your html page

<script language="javascript">
function getTimezoneName() {
timezone = jstz.determine()
return timezone.name();
}
</script>

and call this function from your display tag

How to detect the timezone of a client?

Unfortunately this information is not passed in HTTP headers.

Usually you need cooperating JavaScript to fetch it for you.

Web is full of examples, here is one http://www.coderanch.com/t/486127/JSP/java/Query-timezone

Getting the client's time zone (and offset) in JavaScript

Using getTimezoneOffset()

You can get the time zone offset in minutes like this:

var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01

How to get Client side time zone

code that works for me...

<script type="text/javascript" language="javascript" >
function fnLoad()
{
var objLocalZone = new Date();
var strLocalZone=''+objLocalZone;

var mySplitResult = strLocalZone.split(" ");

var newLocalZone = mySplitResult[5].slice(0,mySplitResult[5].length-2) +':'+mySplitResult[5].slice(mySplitResult[5].length-2,mySplitResult[5].length);
document.getElementById("hdnTimeZone").value = newLocalZone;
//alert('Length : '+newLocalZone);
}
</script>

hidden input

<input type="hidden" id="hdnTimeZone" name="hdnTimeZone"/>

i don't know this is proper way to get client(visitor)'s timezone. but it works fine for me.If anybody have optimum solution this let me know. thanks...



Related Topics



Leave a reply



Submit