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');