gpt4 book ai didi

javascript - 如何将显示的当前时间保存在数据库表中

转载 作者:行者123 更新时间:2023-11-29 08:52:44 27 4
gpt4 key购买 nike

我有这个脚本用于在我的网站页面中显示当前日期和时间:

<script type = "text/javascript">

function startTime() {
var now = new Date();
var h=now.getHours();
var min=now.getMinutes();
var s=now.getSeconds();
var ampm=(now.getHours()>11)?"PM":"AM";
var d=now.getDay();
var y=now.getFullYear();
var mon=now.getMonth();

var endings=["st","nd","rd","th"];
var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1];
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var months=["January","February","March","April", "May", "June","July","August","September", "October", "Novemeber","Decemeber"];
if (h>12) {h-=12}
if (h==0) {h=12};
if (min<10) {min="0"+min}
if (s<10) {s="0"+s}

d=days[d+1];
mon=months[mon];
document.getElementById("time").innerHTML=d+" "+mon+" "+[now.getDate()-6]+", "+y+" "+h+":"+min+":"+s+" "+ampm;
var tim = setTimeout("startTime()",1000);
}
</script>

然后,我有一个文本区域,用户必须在其中输入他或她的学号。单击提交按钮后,当前日期和时间应保存在数据库中。它实际上是针对用户的入出时间。

如何正确地做到这一点?

最佳答案

不要相信客户端时间值。绝对不能保证用户的时钟是准确的,或者不是故意错误设置的。

如果您需要记录“当前时间”,请使用类似的内容

INSERT INTO yourtable (timefield) VALUES (now())
^^^^^

插入一个(据说)更值得信赖的时间戳。

关于javascript - 如何将显示的当前时间保存在数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10757049/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com