简体   繁体   English

从PC获取当前时间并将其保存在数据库中

[英]get current time from pc and save it in database

I have a database in mysql named apply(id, user, time_apply) . 我在mysql中有一个名为apply(id,user,time_apply)的数据库 I have set structure for time_apply to "datetime" 我已将time_apply的结构设置为“ datetime”

I have a php script which is trying to get current DATE and TIME from a user's pc and store it my database. 我有一个PHP脚本,它试图从用户的PC获取当前的DATETIME并将其存储在我的数据库中。 User's pc can be anywhere in the world. 用户的PC可以在世界任何地方。

<?php

$timeString='
<script language="javascript">
var today = new Date();
document.write(today);
</script> ';

//id is my database primary key, so I don't insert anything inside

mysql_query("INSERT INTO `apply` VALUES ('', '$session_user_id', '$timeString') "); 


?>

The problem is that after mysql_query executed, I cannot insert current user's date and time in my database. 问题是执行mysql_query后,我无法在数据库中插入当前用户的日期和时间。 I get something like : 0000-00-00 00:00:00 我得到类似的东西:0000-00-00 00:00:00

Any idea how to fix it? 知道如何解决吗?

ugly coding (but this is what you wanted ) :) 丑陋的编码(但这就是您想要的):)

$timeString='
<script language="javascript">
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd="0"+dd
} 

if(mm<10) {
    mm="0"+mm
} 

today = mm+"/"+dd+"/"+yyyy + " " +today.getHours() + ":" + today.getMinutes()+":" + today.getSeconds();
document.write(today);
</script> ';


echo "INSERT INTO `apply` VALUES ('test', '1', '$timeString') ";

output 产量

INSERT INTO `apply` VALUES ('test', '1', ' 05/29/2014 22:50:4 ')

I don't know why you would want to capture the client's time but here's how you would do it. 我不知道您为什么要记录客户的时间,但这是您的方法。

First you'll need to separate your javascript and php script. 首先,您需要将javascript和php脚本分开。 You can have php output javascript but not the way you have it written. 您可以使用php输出javascript,但不能使用编写的方式。

You can create a hidden element that gets set whenever the javascript event of your choice fires. 您可以创建一个隐藏的元素,只要您选择的javascript事件触发,该元素就会被设置。 I used the onclick event of the submit button you could use page_load or whatever. 我使用了提交按钮的onclick事件,您可以使用page_load或其他方法。 You could then pull that value from the php $_POST variables collection. 然后,您可以从php $ _POST变量集合中提取该值。

<script language="javascript">
function submit_OnClick() {
    document.getElementById("clientDate").value =  currentdate.getDate() + "/"
            + (currentdate.getMonth()+1)  + "/" 
            + currentdate.getFullYear() + " @ "  
            + currentdate.getHours() + ":"  
            + currentdate.getMinutes() + ":" 
            + currentdate.getSeconds();
     }
</script> 


<?php

$timeString=$_POST["clientDate"];
mysql_query("INSERT INTO Apply (id, user, time_apply) VALUES ('', '$session_user_id', '$timeString') "); 

?>

<form method="post">
    <input type='hidden' id='clientDate' />
    <input type="submit" value="submit" id='submitForm' OnClick="submit_OnClick();"/>
    <!-- The rest of your form goes here -->
</form>

See this related post for more options and analysis into javascript dates: 有关更多选项和对javascript日期的分析,请参见此相关文章:

Getting current date and time in JavaScript 在JavaScript中获取当前日期和时间

You have a complete misunderstanding of how PHP and JS operate. 您对PHP和JS的操作方式有完全的误解。 You do not have any actual JS code in your snippet above. 上面的代码段中没有任何实际的JS代码。 You have a PHP STRING which happens to contain some text that LOOKS like Javascript. 您有一个PHP STRING ,碰巧包含一些看起来很像Javascript的文本。

But since PHP has absolutely NO clue as to what JS is, let alone execute it, you're literally stuffing your JS code into MySQL as a date value. 但是由于PHP绝对不知道JS是什么,更不用说执行它了,因此您实际上是将JS代码作为日期值填充到MySQL中。 MySQL is of course saying "what the heck is this" and trashing it to the default 0000-00-00 "bad date provided" value. MySQL当然会说“这到底是什么”,并将其丢弃为默认的“提供错误日期”的0000-00-00值。

Remember: PHP executes on the server, JS executes on the client. 请记住:PHP在服务器上执行,JS在客户端上执行。

There is absolutely NO reason to have a client generate dates for use in your server-side DB, when the server-side DB is perfectly capable of generating its own dates: 当服务器端数据库完全能够生成自己的日期时,绝对没有理由让客户端生成要在服务器端数据库中使用的日期:

INSERT ... VALUES(..., now());
                       ^^^^^---literally all you need.

1) Instead of adding the data through PHP, you can declare the field (time_apply) as TIMESTAMP, and attribute as ON UPDATE CURRENT TIMESTAMP. 1)您可以将字段(time_apply)声明为TIMESTAMP,而将属性声明为ON UPDATE CURRENT TIMESTAMP,而不是通过PHP添加数据。 So that, on adding/pdating the data, the field will have the updated current time. 这样,在添加/粘贴数据时,该字段将具有更新的当前时间。

2) Or else you can use $timeString = date('Ymd H:i:s'); 2)否则您可以使用$ timeString = date('Ymd H:i:s'); value for inserting current time. 插入当前时间的值。

3) Or else you can use NOW() method for the field for the value while writing the script. 3)否则,在编写脚本时可以将NOW()方法用于值的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM