0

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.

X10nD
  • 21,638
  • 45
  • 111
  • 152
  • http://stackoverflow.com/questions/2705067/how-can-i-get-the-users-local-time-instead-of-the-servers-time – kevinamadeus May 28 '13 at 07:36
  • Then the issues is to write this to the php code – X10nD May 28 '13 at 07:38
  • 1
    In Short you cannot do this using php .PHP is server side and cannot get information on client machine . You have to use JS for your purpose – alwaysLearn May 28 '13 at 07:39
  • Ok guys, if I am to do it in JS then how do I throw it into PHP code? – X10nD May 28 '13 at 07:41
  • @X10nD As we all said, you cannot do this with PHP. You could do something like guessing the geolocation from the client IP and provide the time, but the information is not always accurate, and the effort is too large anyway. – kevinamadeus May 28 '13 at 07:41
  • Google found this pretty easily, http://blog.codez.in/php5-time-zone-solution/web-development/2010/07/09 – shapeshifter May 28 '13 at 07:42
  • 1
    @X10nD PHP executes on server, and respond to client. JS then executes on client. Unless the client submit a form to provide time info to the server beforehand, otherwise the PHP would have no clue on the time of the client. – kevinamadeus May 28 '13 at 07:43

2 Answers2

2

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
});
Starx
  • 77,474
  • 47
  • 185
  • 261
1

You are not able to do that unless default timezone function. PHP is server side not client side. You may need to use javascript for that -

var now = new Date();

now.format("m/dd/yy");
// Returns, e.g., 6/09/07

// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM

// You can use one of several named masks
now.format("isoDateTime");
// 2007-06-09T17:46:21

// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
now.format("hammerTime");
// 17:46! Can't touch this!

// When using the standalone dateFormat function,
// you can also provide the date as a string
dateFormat("Jun 9 2007", "fullDate");
// Saturday, June 9, 2007

// Note that if you don't include the mask argument,
// dateFormat.masks.default is used
now.format();
// Sat Jun 09 2007 17:46:21

// And if you don't include the date argument,
// the current date and time is used
dateFormat();
// Sat Jun 09 2007 17:46:22

// You can also skip the date argument (as long as your mask doesn't
// contain any numbers), in which case the current date/time is used
dateFormat("longTime");
// 5:46:22 PM EST

// And finally, you can convert local time to UTC time. Either pass in
// true as an additional argument (no argument skipping allowed in this case):
dateFormat(now, "longTime", true);
now.format("longTime", true);
// Both lines return, e.g., 10:46:21 PM UTC

// ...Or add the prefix "UTC:" to your mask.
now.format("UTC:h:MM:ss TT Z");
// 10:46:21 PM UTC

or try this - https://bitbucket.org/pellepim/jstimezonedetect

How to pass Javascript variable to PHP?

Simply send the date time value to a PHP file using AJAX. Its easy and you can use jQuery for that. Then set cookies/session. That's it!

HADI
  • 2,829
  • 1
  • 26
  • 26