您好,欢迎访问一九零五行业门户网

学习 mysql 格式化日期的函数 DATE_FORMAT,FROM_UNIXTIME,UNIX_TIME

date_format(now(),'%b %d %y %h:%i %p') date_format(now(),'%m-%d-%y') date_format(now(),'%d %b %y') date_format(now(),'%d %b %y %t:%f')
复制代码
结果类似:dec 29 2008 11:45 pm 12-29-2008 29 dec 08 29 dec 2008 16:25:46
2. mysql 数据库中日期与时间函数 from_unixtime(), unix_time() ...实例: date => int(11)
select from_unixtime(date, '%y-%c-%d %h:%i:%s' ) as post_date , date_format(now(), '%y-%c-%d %h:%i:%s' ) as post_date_gmt from `article` where outkey = 'y'
复制代码
1、from_unixtime( unix_timestamp )参数:通常是壹个十位的数字,如:1344887103返回值:有两种,可能是类似 'yyyy-mm-dd hh:mm:ss' 这样的字符串,也有可能是类似于 yyyymmddhhmmss.uuuuuu 这样的数字,具体返回什么取决于该函数被调用的形式。
mysql> select from_unixtime(1344887103); +---------------------------+ | from_unixtime(1344887103) | +---------------------------+ | 2012-08-14 03:45:03 | +---------------------------+ 1 row in set (0.00 sec)
复制代码
2、from_unixtime( unix_timestamp ,format )参数 unix_timestamp :与方法 from_unixtime( unix_timestamp ) 中的参数含义一样;参数 format : 转换之后的时间字符串显示的格式;返回值:按照指定的时间格式显示的字符串;
mysql> select from_unixtime(1344887103,'%y-%m-%d %h:%i:%s'); +-----------------------------------------------+ | from_unixtime(1344887103,'%y-%m-%d %h:%i:%s') | +-----------------------------------------------+ | 2012-august-14th 03:45:03 | +-----------------------------------------------+ 1 row in set (0.00 sec) mysql> select from_unixtime(1344887103,'%y-%m-%d %h:%i:%s'); +-----------------------------------------------+ | from_unixtime(1344887103,'%y-%m-%d %h:%i:%s') | +-----------------------------------------------+ | 2012-08-14th 03:45:03 | +-----------------------------------------------+ 1 row in set (0.00 sec)
复制代码
1、unix_timestamp()返回值:当前时间的unix格式数字串,或者说是 unix 时间戳(从 utc 时间'1970-01-01 00:00:00'开始的秒数),通常为十位,如 1344887103。
mysql> select unix_timestamp(); +------------------+ | unix_timestamp() | +------------------+ | 1344887103 | +------------------+ 1 row in set (0.00 sec)
复制代码
2、unix_timestamp( date )参数:date 可能是个 date 字符串,datetime 字符串,timestape 字符串,或者是一个类似于 yymmdd 或者 yyyymmdd 的数字串。返回:从 utc 时间'1970-01-01 00:00:00'开始到该参数之间的秒数。服务器将参数 date 解释成当前时区的壹个值并且将其转化成 utc 格式的内部时间。客户端则可以自行设置当前时区。当 unix_timestamp() 用于壹个 timestamp 列时,函数直接返回内部时间戳的值;如果你传递壹个超出范围的时间到 unix_timestamp(),它的返回值是零。
mysql> select unix_timestamp(); +------------------+ | unix_timestamp() | +------------------+ | 1344888895 | +------------------+ 1 row in set (0.00 sec) mysql> select unix_timestamp('2012-08-14 16:19:23'); +---------------------------------------+ | unix_timestamp('2012-08-14 16:19:23') | +---------------------------------------+ |1344932363 | +---------------------------------------+ 1 row in set (0.00 sec)
复制代码
注意:如果你使用 unix_timestamp() 和 from_unixtime() 来转换 timestamp 值与 unix 时间戳的值,精度会丢失,因为这个映射在两个方向上不是一一对应的。比如说,由于本地时区的更改,有可能两个 unix_timestamp() 会映射到同壹个 unix 时间戳的值。 from_unixtime() 只会映射到原来的那个时间戳的值上。这里有个例子,在 cet 时区使用 timestamp:
mysql> select unix_timestamp('2005-03-27 03:00:00'); +---------------------------------------+ | unix_timestamp('2005-03-27 03:00:00') | +---------------------------------------+ |1111885200 | +---------------------------------------+ mysql> select unix_timestamp('2005-03-27 02:00:00'); +---------------------------------------+ | unix_timestamp('2005-03-27 02:00:00') | +---------------------------------------+ |1111885200 | +---------------------------------------+ mysql> select from_unixtime(1111885200); +---------------------------+ | from_unixtime(1111885200) | +---------------------------+ | 2005-03-27 03:00:00 | +---------------------------+
复制代码
参考链接: https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp
其它类似信息

推荐信息