Nachshon Schwartz
Nachshon Schwartz

Reputation: 15765

Bash time to mysql datetime

I am trying to get the current time in a bash script and store it in a MySql database. How can I get the current time and convert it to a format that can be saved to a MySql datetime field.

Upvotes: 24

Views: 15209

Answers (2)

ThorSummoner
ThorSummoner

Reputation: 18109

Equivalent of @ruakh 's answer, in expanded form:

date +'%Y-%m-%d %H:%M:%S'

which will print something like:

2014-04-14 11:33:48

Useful if some of the date stamp separator characters aren't valid for one use or another.

Upvotes: 11

ruakh
ruakh

Reputation: 183371

You can write:

date +'%F %T'

which will print something like:

2012-03-04 11:56:54

(But as zerkms says, it's probably better to just use NOW() within MySQL.)

Upvotes: 30

Related Questions