May 28, 2013

Date of user local-system not hosted server – PHP

X10nD’s Question:

When I add this to the code

$today1 = date("d");

it gives me the server date and not the user local system date.

I do not want to use date_default_timezone_set()

How can I get the date of the local user(not web host server) at that point of time using PHP.

PHP runs on the server so using functions like time() and localtime() will not get you the time of the client.

Javascript runs on client’s system thus it can get the time of the client. But how to make the time available to a PHP script is a tricky part.

The answer is AJAX Request, you can send the time from ajax to you script file which will use that value and give you results.

Like

var clientTime = Date.now();
$.get("yourpage.php", { time: clientTime }, function(data) 
   // the response in data
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!