有没有人有一个简单的代码来显示从网络服务器获取的周,日期和时间?
然后另一个代码从本地计算机获取时间?
我正在寻找JS或PHP代码
最佳答案 在服务器端,您可以使用(PHP)
$currentWeekNumber = (int)date('w'); // ISO-8601 week number of the year
$date = date('H:i:s d-m-Y'); // for example (you can use different format)
JS的实现并非如此微不足道.没有内置函数,但这里有一个例子.
function getWeek (getdate) {
var a, b, c, d, e, f, g, n, s, w;
$y = getdate.getFullYear();
$m = getdate.getMonth() + 1;
$d = getdate.getDate();
if ($m <= 2) {
a = $y - 1;
b = (a / 4 | 0) - (a / 100 | 0) + (a / 400 | 0);
c = ((a - 1) / 4 | 0) - ((a - 1) / 100 | 0) + ((a - 1) / 400 | 0);
s = b - c;
e = 0;
f = $d - 1 + (31 * ($m - 1));
} else {
a = $y;
b = (a / 4 | 0) - (a / 100 | 0) + (a / 400 | 0);
c = ((a - 1) / 4 | 0) - ((a - 1) / 100 | 0) + ((a - 1) / 400 | 0);
s = b - c;
e = s + 1;
f = $d + ((153 * ($m - 3) + 2) / 5) + 58 + s;
}
g = (a + b) % 7;
d = (f + g - e) % 7;
n = (f + 3 - d) | 0;
if (n < 0) {
w = 53 - ((g - s) / 5 | 0);
} else if (n > 364 + s) {
w = 1;
} else {
w = (n / 7 | 0) + 1;
}
$y = $m = $d = null;
return w;
}
weeknumber = getWeek(new Date());
UPD:今天,momentjs在JS中有很多关于日期的东西.