php 将当前日期插入数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6880484/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:32:02  来源:igfitidea点击:

Insert current date to the database?

phpmysql

提问by lisovaccaro

How do I insert the current date to my database? I have a column called date to put it on. I want to insert it at the same time I insert this:

如何将当前日期插入到我的数据库中?我有一个名为 date 的专栏可以放在上面。我想在插入它的同时插入它:

$sql="INSERT INTO `Lines` (Text, PID, Position)
VALUES
('$text','$pid','$position')";

Is there a way to automate it in PHPMyAdmin or it's the same to do it this way? Thanks

有没有办法在 PHPMyAdmin 中自动化它,或者以这种方式做同样的事情?谢谢

回答by Explosion Pills

If the table definition has the timestampcolumn default set to CURRENT_TIMESTAMP, you actually don't have to do anything at all. Otherwise, NOW()and CURRENT_TIMESTAMPwill work, as in:

如果表定义将timestamp列默认设置为CURRENT_TIMESTAMP,则您实际上根本不需要做任何事情。否则,NOW()并且CURRENT_TIMESTAMP将工作,如:

INSERT INTO t1 (timestamp_column) VALUES (NOW());

There is a difference between CURRENT_TIMESTAMPand NOW()but it's probably too small to matter to you.

CURRENT_TIMESTAMP和之间存在差异,NOW()但对您来说可能太小了。

phpMyAdmin seems like it has CURRENT_TIMESTAMPas an option when creating a new column.

phpMyAdmin 似乎CURRENT_TIMESTAMP在创建新列时有一个选项。

回答by deceze

INSERT INTO `Lines` (`date`) VALUES (NOW());

回答by Vinayak Garg

Depending on your requirement, you can also do this

根据您的要求,您也可以这样做

$date=date('d.m.y h:i:s');

And then insert $date. I mean if you only want to view the date & time. Otherwise i also recommend time().

然后插入 $date。我的意思是,如果您只想查看日期和时间。否则我也推荐 time()。

回答by Night Owl

The best way to store timestamps is as UNIX timestamp integers in GMT / UTC. This allows you to always know the exact time no matter where you, your server, or your users are located. A bonus is that you can allow your users to set their timezone and display times meaningful to them.

存储时间戳的最佳方法是作为 GMT/UTC 中的 UNIX 时间戳整数。无论您、您的服务器或您的用户身在何处,这都可以让您始终知道确切的时间。一个好处是您可以允许您的用户设置他们的时区并显示对他们有意义的时间。

$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ('" . time() . "')";

or

或者

$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ( UNIX_TIMESTAMP() )";

Be careful if you choose to use NOW() or CURRENT_TIMESTAMP as they are managed by the database server and it's settings. If the server your site is hosted on is in one time zone and you move to another host in another timezone all of your timestamps will be incorrect. Using Unix integers will add a little extra effort wherever you application deals with times but it gives you the most accuracy and the most flexibility.

如果您选择使用 NOW() 或 CURRENT_TIMESTAMP,请小心,因为它们由数据库服务器及其设置管理。如果您的站点所在的服务器位于一个时区,而您移动到另一个时区的另一台主机,则所有时间戳都将不正确。使用 Unix 整数会在您的应用程序处理时间的任何地方增加一点额外的努力,但它为您提供最大的准确性和最大的灵活性。

回答by mpen

Otherwise... if you're using an integer, as is common in PHP, just use time()and insert it's value the same way you inserted the other variables, or use MySQL's UNIX_TIMESTAMP()

否则...如果您使用的是整数,这在 PHP 中很常见,只需time()像插入其他变量一样使用并插入它的值,或者使用 MySQL 的UNIX_TIMESTAMP()