whiterook6
whiterook6

Reputation: 3534

How do I set the time in a MySQL timestamp?

How do I change just the time in a timestamp (say, from 2014-08-08 14:26:15 to 2014-08-08 08:00:00)? In PHP I would probably get the timestamp from MySQL, modify the time, then update the table, but I figured there might be a function for doing so in MySQL alone.

(I know timestamps are not used for this purpose, and that a datetime would be better, but I don't have control over the DB schema.)

Upvotes: 1

Views: 46

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562260

You can format a date (or datetime) into a datetime string, and specify a hard-coded time, ignoring any time component that may be in the original value.

DATE_FORMAT(datecolumn, '%Y-%m-%d 08:00:00')

See http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format for reference doc on that DATE_FORMAT() function.

Upvotes: 2

Related Questions