免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 154274 | 回复: 0
打印 上一主题 下一主题

mysql 将时间戳直接转换成日期时间 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 09:46 |只看该作者 |倒序浏览
date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 
后面的 '%Y%m%d' 主要是将返回值格式化 
例如: 
mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' )  
->20071120 
mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y年%m月%d' )  
->2007年11月20 
UNIX_TIMESTAMP()是与之相对正好相反的时间函数 
 
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date) 
 
  若无参数调用,则返回一个 Unix timestamp ('1970-01-01 00:00:00' GMT 之后的秒数) 作为无符号整数。若用date 来调用 UNIX_TIMESTAMP(),它会将参数值以'1970-01-01 00:00:00' GMT后的秒数的形式返回。date 可以是一个 DATE 字符串、一个 DATETIME字符串、一个 TIMESTAMP或一个当地时间的YYMMDD 或YYYMMDD格式的数字。 
 
例如: 
 
mysql> SELECT UNIX_TIMESTAMP() ; (执行使得时间:2009-08-06 10:10:40) 
->1249524739 
mysql> SELECT UNIX_TIMESTAMP('2009-08-06') ; 
->1249488000 
 
 
[root@Mysql140 ~]# mysql -e "SELECT UNIX_TIMESTAMP('`date -I -d 'a day ago'`') ; "
+------------------------------+
| UNIX_TIMESTAMP('2011-03-02') |
+------------------------------+
|                   1298995200 |
+------------------------------+
 
 
如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? 
Java time  
JavaScript Math.round(new Date().getTime()/1000)  
getTime()返回数值的单位是毫秒  
Microsoft .NET / C# epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000  
MySQL SELECT unix_timestamp(now())  
Perl time  
PHP time()  
PostgreSQL SELECT extract(epoch FROM now())  
Python 先 import time 然后 time.time()  
Ruby 获取Unix时间戳:Time.now 或 Time.new  
显示Unix时间戳:Time.now.to_i  
SQL Server SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())  
Unix / Linux date +%s  
VBScript / ASP DateDiff("s", "01/01/1970 00:00:00", Now())  
其他操作系统  
(如果Perl被安装在系统中) 命令行状态:perl -e "print time"  
如何在不同编程语言中实现Unix时间戳(Unix timestamp) → 普通时间? 
Java String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(Unix timestamp * 1000))  
JavaScript 先 var unixTimestamp = new Date(Unix timestamp * 1000) 然后 commonTime = unixTimestamp.toLocaleString()  
Linux date -d @Unix timestamp  
MySQL from_unixtime(Unix timestamp)  
Perl 先 my $time = Unix timestamp 然后 my ($sec, $min, $hour, $day, $month, $year) = (localtime($time))[0,1,2,3,4,5,6]  
PHP date('r', Unix timestamp)  
PostgreSQL SELECT TIMESTAMP WITH TIME ZONE 'epoch' + Unix timestamp) * INTERVAL '1 second';  
Python 先 import time 然后 time.gmtime(Unix timestamp)  
Ruby Time.at(Unix timestamp)  
SQL Server DATEADD(s, Unix timestamp, '1970-01-01 00:00:00')  
VBScript / ASP DateAdd("s", Unix timestamp, "01/01/1970 00:00:00")  
其他操作系统  
(如果Perl被安装在系统中) 命令行状态:perl -e "print scalar(localtime(Unix timestamp))"
 
 
 

Example:查询member表中近三个月用户登录情况

 

Sql代码
select uid,userid,username,email,FROM_UNIXTIME(lastvisit,'%Y年%m月%d'from members where lastvisit between UNIX_TIMESTAMP('2010-01-06'and UNIX_TIMESTAMP('2010-04-06');  

select uid,userid,username,email,FROM_UNIXTIME(lastvisit,'%Y年%m月%d') from members where lastvisit between UNIX_TIMESTAMP('2010-01-06') and UNIX_TIMESTAMP('2010-04-06');

 


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP