mysql中格式化日期详解
1.DATE_FORMAT()函数用于以不同的格式显示日期/时间数据。
DATE_FORMAT(date,format)
format参数的格式有
例子:
DATE_FORMAT(NOW(),'%b%d%Y%h:%i%p') DATE_FORMAT(NOW(),'%m-%d-%Y') DATE_FORMAT(NOW(),'%d%b%y') DATE_FORMAT(NOW(),'%d%b%Y%T:%f')
输出结果:
Dec29200811:45PM 12-29-2008 29Dec08 29Dec200816:25:46
2.MySQL格式化函数FROM_UNIXTIME()
SELECTFROM_UNIXTIME(date,'%Y-%c-%d%h:%i:%s')aspost_date, date_format(NOW(),'%Y-%c-%d%h:%i:%s')aspost_date_gmt FROM`article`whereoutkey='Y'
1、FROM_UNIXTIME(unix_timestamp)
参数:一般为10位的时间戳,如:1417363200
返回值:有两种,可能是类似'YYYY-MM-DDHH:MM:SS'这样的字符串,也有可能是类似于YYYYMMDDHHMMSS.uuuuuu这样的数字,具体返回什么取决于该函数被调用的形式。
mysql>selectFROM_UNIXTIME(1344887103); +---------------------------+ |FROM_UNIXTIME(1344887103)| +---------------------------+ |2012-08-1403:45:03| +---------------------------+ 1rowinset(0.00sec)
2、FROM_UNIXTIME(unix_timestamp,format)
参数unix_timestamp:与方法FROM_UNIXTIME(unix_timestamp)中的参数含义一样;
参数format:转换之后的时间字符串显示的格式;
返回值:按照指定的时间格式显示的字符串;
mysql>selectFROM_UNIXTIME(1344887103,'%Y-%M-%D%h:%i:%s'); +-----------------------------------------------+ |FROM_UNIXTIME(1344887103,'%Y-%M-%D%h:%i:%s')| +-----------------------------------------------+ |2012-August-14th03:45:03| +-----------------------------------------------+ 1rowinset(0.00sec) mysql>selectFROM_UNIXTIME(1344887103,'%Y-%m-%D%h:%i:%s'); +-----------------------------------------------+ |FROM_UNIXTIME(1344887103,'%Y-%m-%D%h:%i:%s')| +-----------------------------------------------+ |2012-08-14th03:45:03| +-----------------------------------------------+ 1rowinset(0.00sec)
3、判断是不是同一天:
SELECTtbl_gamedata.GameMapName,tbl_playerdata.GameMode,tbl_gamedata.MatchMode,tbl_playerdata.GameResult,SUM(tbl_playerdata.GameIsWin)AStday_winCount, SUM(tbl_playerdata.AssistCount)AStday_assistCount,SUM(tbl_playerdata.KillCount)AStday_killCount, SUM(tbl_player_title.ThreeKill)AStday_threeKill,SUM(tbl_player_title.FourKill)AStday_fourKill,SUM(tbl_player_title.FiveKill)AStday_fiveKill FROMtbl_playerdata LEFTJOINtbl_gamedataONtbl_playerdata.GameID=tbl_gamedata.GameID LEFTJOINtbl_player_titleONtbl_player_title.GameID=tbl_playerdata.GameIDANDtbl_player_title.PlayerID=tbl_playerdata.PlayerID WHEREtbl_playerdata.PlayerID=user_idAND(tbl_playerdata.GameResult=2ORtbl_playerdata.GameResult=3)ANDTO_DAYS(FROM_UNIXTIME(tbl_playerdata.GameStartTime))=TO_DAYS(NOW()) GROUPBYtbl_gamedata.GameMapName,tbl_playerdata.GameMode,tbl_gamedata.MatchMode,tbl_playerdata.GameResult;
其中TO_DAYS(FROM_UNIXTIME(tbl_playerdata.GameStartTime))=TO_DAYS(NOW())就是我们需要的判断
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一些帮助,如果有疑问大家可以留言交流。