hi,欢迎访问本站!
当前位置: 首页Web前端正文

mysql time_format() 函数介绍与用法

墨初 Web前端 750阅读

mysql语句中的 time_format 函数与mysql语句中的date_format() 函数的用法相似,但不同的是 date_format() 函数是对日期进行格式化而time_format函数是对时间进行格式化。

mysql time_format() 函数介绍

time_format():对指定的时间进行定义的格式进行输出。

语法:

time_format(time,format)

参数:

参数描述
time必需,需要格式化时间。
format必需,格式化后的时间格式,需要使用限定符,限定符参考下面的列表。

时间预定义格式符列表:

限定符含义
%H24小时格式的小时,前导加0,例如:00,01..23
%h小时,12小时格式,带前导零,例如:01,02 … 12
%I与 %h 相同
%i分数为零,例如:00,01,… 59
%k24小时格式的小时,无前导零,例如:0,1,2 … 23
%l12小时格式的小时,无前导零,例如:0,1,2 … 12
%pAM或PM,取决于其他时间说明符
%r表示时间,12小时格式hh:mm:ss AM或PM
%S表示秒,前导零,如:00,01,… 59
%s与 %S 相同
%T表示时间,24小时格式hh:mm:ss

mysql time_format() 使用示例

例1:

time_format() 输出带有 am pm 后缀的时间格式

mysql> select time_format('11:21:22','%r');
+------------------------------+
| time_format('11:21:22','%r') |
+------------------------------+
| 11:21:22 AM                  |
+------------------------------+
1 row in set (0.00 sec)

mysql> select time_format('15:21:22','%r');
+------------------------------+
| time_format('15:21:22','%r') |
+------------------------------+
| 03:21:22 PM                  |
+------------------------------+
1 row in set (0.00 sec)

例2:

time_format() 输出 hh:ii:ss 格式的时间

mysql> select time_format('01:21:22','%T');
+------------------------------+
| time_format('01:21:22','%T') |
+------------------------------+
| 01:21:22                     |
+------------------------------+
1 row in set (0.00 sec)

mysql> select time_format('1:21:22','%T');
+-----------------------------+
| time_format('1:21:22','%T') |
+-----------------------------+
| 01:21:22                    |
+-----------------------------+
1 row in set (0.00 sec)

例3:

time_format() 输出 12小时与24小时格式的时间

mysql> select time_format('21:21:22','%h:%i:%s');
+------------------------------------+
| time_format('21:21:22','%h:%i:%s') |
+------------------------------------+
| 09:21:22                           |
+------------------------------------+
1 row in set (0.00 sec)

mysql> select time_format('21:21:22','%H:%i:%s');
+------------------------------------+
| time_format('21:21:22','%H:%i:%s') |
+------------------------------------+
| 21:21:22                           |
+------------------------------------+
1 row in set (0.00 sec)
标签:
声明:无特别说明,转载请标明本文来源!