使用PHP格式化SQL时间戳 [英] Formatting an SQL timestamp with PHP

查看:109
本文介绍了使用PHP格式化SQL时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有时间戳字段的mySQL数据库。目前在测试中只有一个条目,它是

  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 using strtotime :

$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 the date function ; that string is not a valid UNIX Timestamp.

'12-31-69' is probably 1970-01-01, in your locale ; and 1970-01-01 is the Epoch -- the date that corresponds to the 0 UNIX Timestamp.

这篇关于使用PHP格式化SQL时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆