Formatting an SQL timestamp with PHP(使用 PHP 格式化 SQL 时间戳)
问题描述
我有一个带有时间戳字段的 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-01
;1970-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.
这篇关于使用 PHP 格式化 SQL 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP 格式化 SQL 时间戳


- SoapClient 设置自定义 HTTP Header 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01