mysql时间操作(时间差和时间戳和时间字符串的互转)
mysql时间操作(时间差和时间戳和时间字符串的互转)
两个时间差:
MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。
select datediff('2008-08-08', '2008-08-01'); --
select datediff('2008-08-01', '2008-08-08'); -- -7 MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08 注意:timediff(time1,time2) 函数的两个参数类型必须相同。
时间戳和时间字符串的互转 涉及的函数
date_format(date, format) 函数,MySQL日期格式化函数date_format() unix_timestamp() 函数 str_to_date(str, format) 函数 from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime
时间转字符串
select date_format(now(), '%Y-%m-%d'); #结果:2016-01-05
时间转时间戳
select unix_timestamp(now()); #结果:1452001082
字符串转时间
select str_to_date('2016-01-02', '%Y-%m-%d %H');
#结果:2016-01-02 00:00:00
字符串转时间戳
select unix_timestamp('2016-01-02');
#结果:1451664000
时间戳转时间
select from_unixtime(1451997924); #结果:2016-01-05 20:45:24
时间戳转字符串
select from_unixtime(1451997924,'%Y-%d'); //结果:2016-01-05 20:45:24
附表
MySQL日期格式化(format)取值范围。
| 值 | 含义 | |
|---|---|---|
| 秒 | %S、%s | 两位数字形式的秒( 00,01, ..., 59) |
| 分 | %I、%i | 两位数字形式的分( 00,01, ..., 59) |
| 小时 | %H | 24小时制,两位数形式小时(00,01, ...,23) |
| %h | 12小时制,两位数形式小时(00,01, ...,12) | |
| %k | 24小时制,数形式小时(0,1, ...,23) | |
| %l | 12小时制,数形式小时(0,1, ...,12) | |
| %T | 24小时制,时间形式(HH:mm:ss) | |
| %r | 12小时制,时间形式(hh:mm:ss AM 或 PM) | |
| %p | AM上午或PM下午 | |
| 周 | %W | 一周中每一天的名称(Sunday,Monday, ...,Saturday) |
| %a | 一周中每一天名称的缩写(Sun,Mon, ...,Sat) | |
| %w | 以数字形式标识周(0=Sunday,1=Monday, ...,6=Saturday) | |
| %U | 数字表示周数,星期天为周中第一天 | |
| %u | 数字表示周数,星期一为周中第一天 | |
| 天 | %d | 两位数字表示月中天数(01,02, ...,31) |
| %e | 数字表示月中天数(1,2, ...,31) | |
| %D | 英文后缀表示月中天数(1st,2nd,3rd ...) | |
| %j | 以三位数字表示年中天数(001,002, ...,366) | |
| 月 | %M | 英文月名(January,February, ...,December) |
| %b | 英文缩写月名(Jan,Feb, ...,Dec) | |
| %m | 两位数字表示月份(01,02, ...,12) | |
| %c | 数字表示月份(1,2, ...,12) | |
| 年 | %Y | 四位数字表示的年份(2015,2016...) |
| %y | 两位数字表示的年份(15,16...) | |
| 文字输出 | %文字 | 直接输出文字内容 |
mysql时间操作(时间差和时间戳和时间字符串的互转)的更多相关文章
- Python常用时间操作总结【取得当前时间、时间函数、应用等】转载
Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载 2017-05-11 作者:清风乐逍遥 我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...
- Java日期时间操作基础——包含JDK1.8时间操作新特性
JDK1.7日期时间操作 示例小结 public class DateTest { public static final String FORMAT_DATE = "yyyy-MM-dd& ...
- Javascript里面的时间处理:将时间戳或时间对象转成字符串格式
问题背景:想把一个时间直接转成字符串格式 通过查api发现有个toLocaleString(),根据本地时间格式,把 Date 对象转换为字符串 new Date().toLocaleString() ...
- js 时间戳转时间工具类 js时间戳与时间互转
/** * 时间戳格式化函数 * @param {string} format 格式 * @param {int} timestamp 要格式化的时间 默认为当前时间 * @return {strin ...
- mysql 日期操作 增减天数、时间转换、时间戳(转换)
http://hi.baidu.com/juntao_li/item/094d78c6ce1aa060f6c95d0b MySQL datediff(date1,date2):两个日期相减 date1 ...
- mysql 日期操作 增减天数、时间转换、时间戳(转)
转自http://www.cnblogs.com/wenzichiqingwa/archive/2013/03/05/2944485.html http://hi.baidu.com/juntao_l ...
- paip.日期时间操作以及时间戳uapi php java python 总结
paip.日期时间操作以及时间戳uapi php java python 总结 ///uapi Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | ...
- mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间
mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间,我们在mysql里面他封装了一个内置的时间戳转化的函数,比如我们现在的时间戳是:1458536709 ,"%Y-%m-%d&quo ...
- MySQL时间戳和时间格式转换函数
MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...
随机推荐
- [BZOJ1260][CQOI2007]涂色paint 区间dp
1260: [CQOI2007]涂色paint Time Limit: 30 Sec Memory Limit: 64 MB Submit: 1575 Solved: 955 [Submit][S ...
- JD2
Business Requirement Support l Develops and communicates plan to manage vendor review of requirement ...
- Number of Connected Components in an Undirected Graph -- LeetCode
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- unity3d Billboard
CameraFacingBillboard CameraFacingBillboard From Unify Community Wiki Jump to: navigation, searc ...
- .xcodeprok cannot be opened because the project file cannot be parsed
用svn更新代码后,打开xcode工程文件出现 xxx..xcodeproj cannot be opened because the project file cannot be parsed. 这 ...
- requestFeature() must be called before adding content
错误:requestFeature() must be called before adding content 原因: Android去掉标题栏点菜单键程序崩溃 在安卓以前的版本中,我们会使用以下两 ...
- 体验VisualStudio 2013中的内存分析功能
内存分析一直是个比较令人头痛的问题,Visual Studio 2013中就集成了一个内存分析的功能,可以方便我们进行分析内存的占用情况.本文将简单的介绍一下如何使用这个功能. 首先以一个简单的程序为 ...
- ruby ide
uby 是个动态面向对象.通用的编程语言.它支持多种编程范式,包括面向功能和面向对象.同时它也包括一个动态类型系统和自动内存管理. 编辑器和集成开发环境(IDE)有些不同,编辑器是用来添加,编辑,查看 ...
- 关于ElasticSearch默认窗口结果集参数max_result_window修改
在Linux服务器中执行如下命令 curl -XPUT http://192.168.46.163:9200/t_order/_settings -d '{ "index" : { ...
- iptables 使用场景
25 Most Frequently Used Linux IPTables Rules Examples by RAMESH NATARAJAN on JUNE 14, 2011 At a firs ...