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
Difference Between "Process.Stdout.Write" and "Console.Log" in Node.Js
Google Maps Move Marker with Lat/Lng from Ajax Success Returned Data
How to Update States 'Onchange' in an Array of Object in React Hooks
How to Split a String into an Array of Characters
Can't Trigger Click with Jquery in a Chrome Extension
What's Pros and Cons: Putting JavaScript in Head and Putting Just Before the Body Close
JSON Left Out Infinity and Nan; JSON Status in Ecmascript
Logging Clientside JavaScript Errors on Server
How to Send a Message to a Particular Client with Socket.Io
How to Access Google Sheet Spreadsheets Only with JavaScript
JavaScript - How to Make This Code Work
Get Position/Offset of Element Relative to a Parent Container
How to Request the Garbage Collector in Node.Js to Run
How to Access Object Property with Invalid Characters
Is There a Generic Way to Set State in React Hooks? How to Manage Multiple States