PHP:将当前时间戳插入 SQL Server 2005
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/239414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Inserting the current timestamp into SQL Server 2005
提问by robsoft
How do I insert a current_timestamp into an SQL Server 2005 database datable with a timestamp column?
如何将 current_timestamp 插入到带有时间戳列的 SQL Server 2005 数据库中?
It should be simple but I cannot get it to work. Examples would be much appreciated.
它应该很简单,但我无法让它工作。示例将不胜感激。
回答by robsoft
if you can execute a query from PHP then it should just be a matter of using 'getdate()' ;
如果您可以从 PHP 执行查询,那么它应该只是使用 'getdate()' 的问题;
update MyTable set MyColumn=getdate();
or
或者
insert into MyTable (MyColumn) values (getdate());
回答by Tom Haigh
The column datatype should be datetime
列数据类型应该是日期时间
You can either get the database to work out the date:
您可以让数据库计算出日期:
$sql = 'INSERT INTO tablename (fieldname) VALUES (getdate())';
or get PHP to work out the date
或让 PHP 计算出日期
$sql = 'INSERT INTO tablename (fieldname) VALUES (\'' . date('Y-m-d H:i:s') . '\')';
then something like mssql_execute($sql);but this depends on how you are connecting to your database
然后类似mssql_execute($sql);但这取决于您如何连接到数据库
回答by davibq
To insert data into a timeStamp field, I think you have to use DEFAULT.
For example:
要将数据插入时间戳字段,我认为您必须使用DEFAULT. 例如:
INSERT INTO User(username, EnterTS) VALUES ('user123', DEFAULT)
where EnterTSis a TimeStamp field
EnterTS时间戳字段在哪里
回答by Vinko Vrsalovic
You just use the normal mechanism to execute your queries into SQL Server, and use what follows.
您只需使用常规机制在 SQL Server 中执行查询,并使用以下内容。
The current_timestamp function returns it
current_timestamp 函数返回它
insert into table (creation_date) VALUES (current_timestamp)
Complete example
完整示例
CREATE table timestamp_test (creation_timestamp datetime)
INSERT INTO timestamp_test (creation_timestamp) VALUES (current_timestamp)
SELECT * FROM timestamp_test
DROP TABLE timestamp_test
回答by HLGEM
If you explain the error you are receiving (and post your insert or update code), it might make it easier to see what your problem is. One possible issue is that you must be inserting into a datetime field (or in 2008 you could use a date field as well). Occasionally people think that they can insert the current date into a timestamp field but even though the name of this data type seems as if it should be date related, this data type actaully has nothing to do with dates and times and cannot accept date data.
如果您解释您收到的错误(并发布您的插入或更新代码),可能更容易查看您的问题所在。一个可能的问题是您必须插入日期时间字段(或者在 2008 年您也可以使用日期字段)。有时人们认为他们可以将当前日期插入时间戳字段,但即使这种数据类型的名称看起来应该与日期相关,但这种数据类型实际上与日期和时间无关,不能接受日期数据。
回答by HPWD
I realize this is an old post but I ran across this and figured others might too.
我意识到这是一个旧帖子,但我遇到了这个并认为其他人也可能。
What I do is to create a field in the table using the datatype of datetime and then for the column properties for the default Value or binding, I enter getdate(). Every record that gets entered into the table now has a date and time stamp.
我所做的是使用 datetime 数据类型在表中创建一个字段,然后对于默认值或绑定的列属性,我输入getdate(). 现在输入到表中的每条记录都有一个日期和时间戳。

