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 时间戳
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
