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.
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.
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.
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.
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' .
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With