Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash time to mysql datetime

Tags:

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.

like image 259
Nachshon Schwartz Avatar asked Mar 04 '12 19:03

Nachshon Schwartz


People also ask

How can get date in dd mm yyyy format in MySQL?

The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d. %m.

What is the datetime format in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

Does MySQL datetime store timezone?

In short, “ MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME .)” In other words, timezone information is lost in DATETIME columns.

How does MySQL store time?

For the time interval, you can use the 'D HH:MM:SS' format where D represents days with a range from 0 to 34. A more flexible syntax is 'HH:MM' , 'D HH:MM' , 'D HH' , or 'SS' . If you use the delimiter:, you can use 1 digit to represent hours, minutes, or seconds. For example, 9:5:0 can be used instead of '09:05:00' .


2 Answers

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.)

like image 82
ruakh Avatar answered Sep 20 '22 23:09

ruakh


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.

like image 9
ThorSummoner Avatar answered Sep 19 '22 23:09

ThorSummoner