简体   繁体   English

如何生成日期时间并将其插入到MySQL数据库(PHP)中

[英]How to generate and insert datetime into a mysql database (PHP)

Hey I need to insert current datatime into mysql database.. this is the format: 嘿,我需要将当前数据时间插入到mysql数据库中。

0000-00-00 00:00:00 0000-00-00 00:00:00

Most easily done with MySQL directly using the NOW() function. 直接使用NOW()函数使用MySQL最容易完成。

INSERT INTO tbl (datecol) VALUES (NOW());

If you need PHP to produce a value other than the exact current timestamp, use the format: 如果您需要PHP产生不同于当前时间戳的值,请使用以下格式:

date('Y-m-d H:i:s', $some_unix_timestamp);

As a MySQL query: 作为MySQL查询:

INSERT INTO table (fieldname) VALUES (NOW())

And wrapped in PHP: 并包装在PHP中:

$db = new PDO($dsn, $user, $password);
$db->query("INSERT INTO table (fieldname) VALUES (NOW())");

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

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