Home > OS >  How do I save a timestamp to a mysql database?
How do I save a timestamp to a mysql database?

Time:02-07

I have a table in mysql for people's posts. But I'd like to be able to get the time the post was made? How do I do it? Any help will be appreciated. The language I'm using is PHP and I'm using prepared statements. Is it as simple as specifying a column in the database with the datatype TIMESTAMP? And how do I read a timestamp and convert it to a date?

CodePudding user response:

  $timestamp = date("Y-m-d H:i:s");

check your table setup to confirm that the field is set to NOT NULL with a default of CURRENT_TIMESTAMP and above code will return you current date and time which will be in string format. Which you can push in to table.

CodePudding user response:

enter image description here

When specifying a column in the database, Use datetime and as default, select current time

If you want to use this data, just extract from database and use date('y/m/d', strtotime($time));

CodePudding user response:

If the column is NOT NULL with DEFAULT CURRENT_TIMESTAMP you don't need to mention the column in the INSERT.

  •  Tags:  
  • Related