使用 PHP 格式化 SQL 时间戳

2022-01-13 00:00:00 timestamp date php mysql

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

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

我的最终结果显示为

12-31-69

有人知道为什么吗?

忽略那个编辑...notepad++ 的 FTP 插件超时,不幸的是,它在无法同步时不会显示错误.

editedit: disregard that edit... the FTP addon for notepad++ timed out and unfortunately doesn't display an error when it can't synch.

推荐答案

date 函数需要 UNIX 时间戳 作为其第二个参数 -- 这意味着您必须将从数据库获取的日期转换为 UNIX 时间戳,这可以使用 strtotime :

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

你会得到:

02-20-2010


您将 '2010-02-20 13:14:09' 字符串传递给 date 函数;该字符串不是有效的 UNIX 时间戳.


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' 在您的语言环境中可能是 1970-01-011970-01-01 是 Epoch -- 对应于 0 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.

相关文章