惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Martin Fowler
Martin Fowler
爱范儿
爱范儿
IT之家
IT之家
Last Week in AI
Last Week in AI
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
The Cloudflare Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog

博客园 - ξσ Dicky σξ

管理学常见定律 江苏省盐城市响水县运河镇开源路商业街商铺及套间招商招租出售 苏州鼎耀文化传媒有限公司 项目管理专用中英文术语词汇 Windows Server 2008 r2 64bit 运行ASP提示ADODB.Connection error ‘800a0e7a’错误解决办法 解决IIS7运行ASP提示错误:An error occurred on the server when processing the URL. Please contact the system administrator 安装Adobe Flash CS5出错的解决办法(Exit Code: 7 ERROR: Unable to get root from inChildPath) Windows 7下破解Visual Studio 2008试用版的评估期限制 PHP/ASP.NET/ASP网站定制开发 跨服务器、数据库、表联合查询 安装完Visual Team System 2008 Team Explorer出错解决办法 查询重复记录的SQL语句 PHP 正则判断中文 UTF-8 & GBK - ξσ Dicky σξ 招聘资深PHP开发架构师 VS2005/2008中清除最近打开项目的方法 Debugging techniques for PHP programmers PHP 程序员的调试技术 - ξσ Dicky σξ 如何调试Php? - ξσ Dicky σξ Javascript跨域和Ajax跨域解决方案(转)
MySQL的FROM_UNIXTIME()和UNIX_TIMESTAMP()函数的区别
ξσ Dicky σξ · 2010-02-05 · via 博客园 - ξσ Dicky σξ

from_unixtime()是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

如何在不同编程语言中获取现在的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被安装在系统中)