将mysql时间戳转换为实际日期和时间?

发布于 2024-10-30 09:58:05 字数 228 浏览 4 评论 0原文

我将日期存储在 mysql 表中,它们设置为在每行中存储为当前时间戳,并存储如下:

2010-05-29 01:17:35

但我想做的是以某种方式使用 PHP 来分隔所有内容并创建一个更像这样的日期:

2010 年 5 月 29 日上午 1:17

任何人都可以至少指导我走上正确的道路吗?非常感谢任何帮助!

I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:

2010-05-29 01:17:35

but what i am trying to do is somehow use PHP to be able to seperate everything and create a date more like:

May 29 2010 1:17 AM

can anyone at least direct me in the right path that i should take. any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飞烟轻若梦 2024-11-06 09:58:05
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));

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

echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));

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

GRAY°灰色天空 2024-11-06 09:58:05

您有两种解决方案:

In PHP code, this would mean using :

echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));

或者:

$dt = new DateTime('2010-05-29 01:17:35');
echo $dt->format('M j Y g:i A');

`strtotime` + `date` is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates *(from 1970 to 2038, if using 32 bits integers)*.

另一方面,使用 DateTime 类,您可以工作的日期范围没有限制和。

You have two solutions :

In PHP code, this would mean using :

echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));

Or :

$dt = new DateTime('2010-05-29 01:17:35');
echo $dt->format('M j Y g:i A');

`strtotime` + `date` is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates *(from 1970 to 2038, if using 32 bits integers)*.

ON the other hand, using the DateTime class, there will be no limit to the range of dates you can work with.

心奴独伤 2024-11-06 09:58:05

如果您的表中有一个 DATETIME 字段,并且您只需要日期字段,则:

SELECT DATE_FORMAT(timestamp,'%M %D, %Y') FROM Mytable;

或:

SELECT DATE_FORMAT(timestamp,'%Y-%m-%d') FROM Mytable;

其中 timestamp 是您的列名称。

If you have a DATETIME field in your table and you only want the date field, then:

SELECT DATE_FORMAT(timestamp,'%M %D, %Y') FROM Mytable;

or:

SELECT DATE_FORMAT(timestamp,'%Y-%m-%d') FROM Mytable;

where timestamp is your column name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文