后面的 '%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');
