使用PHP格式化SQL时间戳 [英] Formatting an SQL timestamp with PHP
问题描述
2010-02-20 13:14:09
我从数据库中拉出并使用
echo date(mdY,$ r ['newsDate'])
我的最终结果显示为
12-31-69
/ pre>
任何人都知道为什么?
编辑:
editit:
忽略编辑... notepad ++的FTP插件超时,不幸的是,当它不能同步时不会显示错误。解决方案
date
函数期望 UNIX时间戳作为其第二个参数 - 这意味着您必须将您的日期转换为从DB获取到UNIX时间戳,可以使用strtotime
:$ db ='2010-02-20 13:14:09'
$ timestamp = strtotime($ db);
echo date(m-d-Y,$ timestamp);
你会得到:
02-20-2010
您正在将'2010-02-20 13:14:09'
字符串传递给date
函数;该字符串不是有效的UNIX时间戳。
'12 -31-69
'可能1970-01-01
,在你的地区;和1970-01-01
是Epoch - 对应于0 UNIX时间戳的日期。I have a mySQL database with a timestamp field. It currently only has one entry while I'm testing, it is
2010-02-20 13:14:09
I am pulling from the database and using
echo date("m-d-Y",$r['newsDate'])
My end result is showing as
12-31-69
Anyone know why?
Edit: editedit: disregard that edit... the FTP addon for notepad++ timed out and unfortunately doesn't display an error when it can't synch.
解决方案The
date
function expects an UNIX timestamp as its second parameter -- which means you have to convert the date you get from the DB to an UNIX timestamp, which can be done usingstrtotime
:$db = '2010-02-20 13:14:09'; $timestamp = strtotime($db); echo date("m-d-Y", $timestamp);
And you'll get :
02-20-2010
You were passing the'2010-02-20 13:14:09'
string to thedate
function ; that string is not a valid UNIX Timestamp.
'12-31-69
' is probably1970-01-01
, in your locale ; and1970-01-01
is the Epoch -- the date that corresponds to the 0 UNIX Timestamp.这篇关于使用PHP格式化SQL时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!