php - 使用 php 或 MySql 将时间戳转换为 DateTime

标签 php mysql datetime

我已经找了 40 多分钟了,但还是找不到。

我收到这个查询:

$query = "SELECT id, title, timestamp FROM adafruit_articles ORDER BY timestamp ASC";

如果我在 php 中回显时间戳,那么我会得到:

[timestamp] => 2012-04-30 19:14:59

我想获取带有前导零的月份。 我可以将时间戳转换为整数,但不能转换为 DateTime

如果我是正确的,最后一步将是:

date_format($date, "m")

但是我怎样才能获得它的DateTime呢?

如果 MySQL 只能返回月份,我还需要在某个地方使用整个日期,因此仅从 MySQL 返回月份并不是我想要的。

最佳答案

我想这就是你想要的:

$unix_timestamp = strtotime($row['timestamp']);
$date = date('m', $unix_timestamp);
<小时/>

m: Numeric representation of a month, with leading zeros

http://php.net/manual/en/function.date.php

关于php - 使用 php 或 MySql 将时间戳转换为 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10879829/

相关文章:

c# - NHibernate 无法使用 MySql 编译映射文件

mysql - wget解析并插入mysql

php - 从 SQL BLOB 输出图像

python-3.x - 将秒数的numpy数组转换为分钟和秒

php - SESSION检查后无法登录

php - Laravel Input::file 返回空数组,而 $_FILES 有数据

Python 2.7 : Pandas datetime does not work for future dates?

.net - DateTime.ToUniversalTime 和 TimeZoneInfo.ConvertTimeToUtc 之间有什么区别

php - 更改 http header 信息以隐藏服务器信息

php 检查 session 是否足够安全?