mysql数据库时间戳函数
2015-09-30 13:48
531 查看
1.关于mysql时间戳的限制
目前timestamp所能表示的范围在1970 - 2038之间。超过这个范围得到的时间将会溢出得到的时间是null.眼看2038已不远,在设计数据库的时候,也会考虑把timestamp换成其他类型来定义。
mysql> select from_unixtime(0);
+---------------------+
| from_unixtime(0) |
+---------------------+
| 1970-01-01 08:00:00 |
+---------------------+
mysql> select from_unixtime(2147483650);
+---------------------------+
| from_unixtime(2147483650) |
+---------------------------+
| 2038-01-19 11:14:10 |
+---------------------------+
1 row in set (0.00 sec)
2.unix_timestamp
将时间转化为时间戳。(date类型数据转换成timestamp形式整数)
没传时间参数则取当前时间的时间戳
mysql>select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1443577432 |
+------------------+
1 row in set (0.01 sec)
mysql>select unix_timestamp('2015-10-01 10:10:10');
+---------------------------------------+
| unix_timestamp('2015-10-01 10:10:10')
+---------------------------------------+
| 1443665410 |
+---------------------------------------+
1 row in set (0.00 sec)
3.from_unixtime
将timestamp形式整数转化为 date类型
mysql> select from_unixtime(1439072360);
+---------------------------+
| from_unixtime(1439072360) |
+---------------------------+
| 2015-08-09 06:19:20 |
+---------------------------+
1 row in set (0.00 sec)
当然也可以指定输出的时间格式:
mysql> select from_unixtime(1439072360,'%Y%m%d');
+------------------------------------+
| from_unixtime(1439072360,'%Y%m%d') |
+------------------------------------+
| 20150809 |
+------------------------------------+
目前timestamp所能表示的范围在1970 - 2038之间。超过这个范围得到的时间将会溢出得到的时间是null.眼看2038已不远,在设计数据库的时候,也会考虑把timestamp换成其他类型来定义。
mysql> select from_unixtime(0);
+---------------------+
| from_unixtime(0) |
+---------------------+
| 1970-01-01 08:00:00 |
+---------------------+
mysql> select from_unixtime(2147483650);
+---------------------------+
| from_unixtime(2147483650) |
+---------------------------+
| 2038-01-19 11:14:10 |
+---------------------------+
1 row in set (0.00 sec)
2.unix_timestamp
将时间转化为时间戳。(date类型数据转换成timestamp形式整数)
没传时间参数则取当前时间的时间戳
mysql>select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1443577432 |
+------------------+
1 row in set (0.01 sec)
mysql>select unix_timestamp('2015-10-01 10:10:10');
+---------------------------------------+
| unix_timestamp('2015-10-01 10:10:10')
+---------------------------------------+
| 1443665410 |
+---------------------------------------+
1 row in set (0.00 sec)
3.from_unixtime
将timestamp形式整数转化为 date类型
mysql> select from_unixtime(1439072360);
+---------------------------+
| from_unixtime(1439072360) |
+---------------------------+
| 2015-08-09 06:19:20 |
+---------------------------+
1 row in set (0.00 sec)
当然也可以指定输出的时间格式:
mysql> select from_unixtime(1439072360,'%Y%m%d');
+------------------------------------+
| from_unixtime(1439072360,'%Y%m%d') |
+------------------------------------+
| 20150809 |
+------------------------------------+
相关文章推荐
- mysql多实例添加
- Mysql ——1045-Access denied for user 'root'@'localhost'(using password:YES)
- mysql命令行查看表结构,字段等信息 [mysql]
- cmake安装mysql
- mysql连接远程数据库出现 ERROR 2013 (HY000) Lost connection to MySQL server at 'reading authorization packet',
- mysql免安装的配置
- MySQL的自增主键
- osx安装启动mysql
- mysql修改表、字段、库的字符集
- Code First for Mysql 错误:未为提供程序“MySql.Data.MySqlClient”找到任何 MigrationSqlGenerator。请在目标迁移配置类中使用 SetSql
- 如果使用得当,MySQL 也可以化身 NoSQL
- 如果使用得当,MySQL 也可以化身 NoSQL
- MySQL中的备份和恢复
- mysql基础知识-操作表数据
- mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法
- mysql基础知识
- MySQL5.6 GTID新特性实践
- CentOS使用MySQL的详细步骤,及C API编程
- 《php和mysql web开发》笔记——第5章 代码重用与函数编写
- MySQL----cluster安装