mind set
mind set

Reputation: 133

How to insert the current date on my table?

How to insert this date format on my table: 30/12/2018?

If it's impossible, then how can i turn this date format: 2018-12-30 12:10:00 to 30/12/2018 on a echo?

Upvotes: 0

Views: 142

Answers (2)

GMB
GMB

Reputation: 222682

You should proceed as follows :

  • ensure that the field where you store the date is of type datetime or date
  • use function STR_TO_DATE to convert strings to dates before writing to database
  • use function DATE_FORMAT to format the datetime values to the relevant format when reading form database.

Here is a small example of CREATE/INSERT/SELECT :

CREATE TABLE mytable (
    mydate    datetime
);

INSERT INTO mytable 
VALUES (STR_TO_DATE('30/12/2018', '%d/%m/%Y'));

SELECT DATE_FORMAT(mydate, '%d/%m/%Y')
FROM mytable;

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1271111

Store the date/time in the native format (i.e. as a datetime or date). Then, use date_format() to convert it to the format you want on output:

select date_format(datecol, '%d/%m/%Y')

Upvotes: 3

Related Questions