网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > MySQL
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 114.238.173.71
Old 2014-11-27, 01:11 PM
Alayah Alayah is offline
初级会员
 
Join Date: 2009-04-21
Posts: 1
Alayah 现在声名狼藉
Default mysql 将时间戳直接转换成日期时间

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');
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql 帮助文档使用 Haddam MySQL 0 2014-11-26 06:43 PM
如何导入导出MySQL数据库 topvip 建站交流 0 2006-10-10 07:43 AM
使用XAMPP轻松建站(Linux版) smiling 服务器环境搭建 0 2006-05-13 04:01 AM
mysql安装与简单使用 sunshine 服务器环境搭建 0 2006-01-11 07:29 PM
winxp下安装全新的apache,php(5.0),mysql(5.0)... topvip 服务器环境搭建 0 2006-01-08 10:28 PM

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 10:54 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.