qadenza
qadenza

Reputation: 9293

How to insert curent datetime using specific format

CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `user` varchar(9) NOT NULL,
  `post` text NOT NULL,
  `date` varchar(14) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE = MyISAM  DEFAULT CHARSET=utf8;

table created

INSERT INTO `posts` ( 'user', 'post', 'date') VALUES
('she', 'dolphin', '???');
('me', 'chandra'), '???');

Instead of ??? I need current datetime in the following format:day.month. hour:min

For example for today and now: 03.01. 00:34

Upvotes: 0

Views: 44

Answers (1)

Pekka
Pekka

Reputation: 449485

That a bad idea, because you are storing the data

  • in a format that can't be ordered
  • in a format that mySQL can't optimize in any meaningful way
  • without the year of the date

Use a proper mySQL DATETIME field instead, and format the output using DATE_FORMAT when you make the query:

SELECT DATE_FORMAT(`date`, "%d.%m. %H:%i") FROM `posts`;

Upvotes: 3

Related Questions