Asaf
Asaf

Reputation: 557

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:

0000-00-00 00:00:00

Upvotes: 0

Views: 9406

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270775

Most easily done with MySQL directly using the NOW() function.

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

If you need PHP to produce a value other than the exact current timestamp, use the format:

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

Upvotes: 6

Adam Hopkinson
Adam Hopkinson

Reputation: 28793

As a MySQL query:

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

And wrapped in PHP:

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

Upvotes: 2

Related Questions