0

Hey I need to insert current datatime into mysql database.. this is the format:

0000-00-00 00:00:00

1
  • There must be dozens of duplicate questions, but I'm having difficulty locating one. If someone can locate an appropriate dupe I'll remove my answer. Commented Sep 27, 2011 at 19:37

2 Answers 2

6

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@Asaf What columns does your users table have? If it worked before adding NOW() that would imply that your users table has only 5 columns - the empty one '', $username, $email, $md5password, $active_code.
2

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())");

1 Comment

UNIX_TIMESTAMP() returns Unix time, an integer, not the correct MySQL date format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.