用户本地系统的日期没有托管服务器- PHP


Date of user local-system not hosted server - PHP

当我添加到代码

$today1 = date("d");

它给我服务器日期,而不是用户本地系统日期。

我不想使用date_default_timezone_set()

如何使用PHP在那个时间点获得本地用户(不是web主机服务器)的日期。

PHP运行在服务器上,因此使用time()和localtime()等函数将无法获得客户端的时间。

Javascript运行在客户端的系统上,因此它可以得到客户端的时间。但是如何使时间对PHP脚本可用是一个棘手的部分。

答案是AJAX请求,您可以将AJAX的时间发送到脚本文件,脚本文件将使用该值并给出结果。

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

您不能这样做,除非默认时区函数。PHP是服务器端而不是客户端。你可能需要使用javascript -

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

或者试试这个- https://bitbucket.org/pellepim/jstimezonedetect

如何将Javascript变量传递给PHP?

只需使用AJAX将日期时间值发送到PHP文件。这很简单,你可以使用jQuery。然后设置cookie/session。就是这样!