How to Obtain the User's Local Time Rather Than the Server's Time
In certain situations, web applications may require access to the user's local time rather than the server's time. By default, the PHP date() function retrieves the time from the server. To overcome this limitation and obtain the time on the client side, we present an alternative approach.
Solution:
Utilizing JavaScript, you can obtain the user's local time as follows:
echo '<script type="text/javascript"> var x = new Date() document.write(x) </script>';
This code snippet creates a JavaScript variable x that holds the current date and time on the client's machine. By embedding this code in your web page, you can retrieve and display the user's local time, regardless of the server's time zone or location.
The above is the detailed content of How to Get the User's Local Time in a Web Application?. For more information, please follow other related articles on the PHP Chinese website!