please let me know is anybody aware of the below doubt.

php taking hosting server date and time but am in need to find the client time and date who accesing the site.

otherwise how to find the date & time in server time zone .

4 Years
Discussion Span
Last Post by eburlea

The simple solution is to ask visitors what timezone they're in and calculate the date from the offset. A less simple solution is to use Javascript/AJAX to acquire the date-time from the client side and pass it to the server for processing.


PHP doesn't know the date and time of the client machine, since it is run on server. Use javascript to get the client's datetime and push it into the database through an AJAX call



I have the same problem and I have solved just half of it.

I have downloaded a javascript plugin 'jsTimezoneDetect' (https://bitbucket.org/pellepim/jstimezonedetect/downloads) and linked it in the header. Then I wrote the following code that is returning the timezone:

<script type="text/javascript">
    var timezone = jstz.determine();
    var user_timezone = timezone.name();

How can I pass the value of the variable user_timezone to PHP in the same page? I do not want to use method $_GET. I have tried with Ajax, but without success.

I am using Zend Framework and I need to pass the value from View to Controller.



After hours of googling, I have found the answer on http://www.satya-weblog.com/2007/05/php-and-javascript-cookie.html

in View

<script type="text/javascript">
    var timezone = jstz.determine();
    document.cookie = 'user_timezone=' + timezone.name();

in Controller

$request = new Zend_Controller_Request_Http();
$user_timezone = $request->getCookie('user_timezone');
    echo 'User Timezone is ' . $user_timezone . '.';
} else {
    echo 'The cookie has not been set yet.';

Yes, it is working.

Actually I make some settings in Bootstrap for language, timezone, etc. The javascript code for getting user timezone I put in Layout. With the codes above I am able to catch the cookie value set with javascript and use in PHP.

But if there are better solutions, I would like to know them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.