Bash 时间到 mysql 日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9558278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:44:18  来源:igfitidea点击:

Bash time to mysql datetime

mysqlbashdatetimetime

提问by Nachshon Schwartz

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.

我试图在 bash 脚本中获取当前时间并将其存储在 MySql 数据库中。如何获取当前时间并将其转换为可以保存到 MySql 日期时间字段的格式。

回答by ruakh

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

(但正如 zerkms 所说,NOW()在 MySQL 中使用可能更好。)

回答by ThorSummoner

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

等效于@ruakh 的答案,扩展形式:

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.

如果某些日期戳分隔符字符对于一种用途或另一种用途无效,则很有用。