00 Votes

PHP: Date Format for MySQL INSERT

Question by Compi | 2018-05-12 at 20:43

I want to write a date into a DATETIME or TIMESTAMP column of a MySQL table in my database. Which format should I use for PHP's date() function for MySQL to accept the date and insert it correctly?

ReplyPositiveNegative
0Best Answer0 Votes

MySQL requires a date in the format YYYY-MM-DD SS-MM-SS. You get this format with the format string "Y-m-d H:i:s":

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

$query = "UPDATE tab SET dat = '$date' WHERE id = 1";

When inserting, make sure to put the date in quotation marks. So for example dat = '$date' as in the example above.

Incidentally, you can also omit the time part and, for example, only use date("Y-m-d"). Then MySQL uses 00:00:00 as a timestamp.
2018-05-12 at 22:29

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Determine Week Number

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.