Guys,

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 .

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

Member Avatar for diafol

Just be aware that users can pass any type of rubbish to your server. They could lie!

Hello!

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();
</script>

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.

Thanks.

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();
</script>

in Controller

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

Does it suit your needs? Is it working?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.