1

Mysql table has a column DatePosted with the Datatype as TIMESTAMP.

I am trying to insert the current TIMESTAMP into the table using PHP. However the below code does not seem to work i get 0000-00-00 00:00:00. Is the format wrong how do i get the current timestamp to be inserted in mysql

TIMESTAMP.php file

date_default_timezone_set('America/Los_Angeles');

echo date("j of F Y, \a\\t g.i a", time());

mysqli_query($con,"INSERT INTO cfv_timestamp (DatePosted) VALUES ('".date("j F Y, \a\\t g.i a", time())."' )");

.

CREATE TABLE `cfv_timestamp (
  `DatePosted` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
3
  • 4
    Why don't you use NOW() ? ... mysqli_query($con,"INSERT INTO cfv_timestamp (DatePosted) VALUES (NOW())"); Commented Mar 28, 2014 at 5:24
  • Or INSERT INTO cfv_timestamp (DatePosted) VALUES (DEFAULT) or even INSERT INTO cfv_timestamp () VALUES () since DatePosted has a default of CURRENT_TIMESTAMP. Commented Mar 28, 2014 at 5:29
  • @BillKarwin i did not want to use the Default CURRENTTIMESTAMP since i wanted the timezone of America/Los_Angeles Commented Mar 28, 2014 at 5:36

1 Answer 1

3

You should check mysql Timestamp format and change yours.

mysqli_query($con,"INSERT INTO cfv_timestamp (DatePosted) 
    VALUES ('".date("Y-m-d H:i:s", time())."' )");

Also you could set default value for Timestamp type CURRENT_TIMESTAMP

Sign up to request clarification or add additional context in comments.

Comments

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.