php 将mysql时间戳转换为实际日期和时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5547252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert mysql timestamp into actual date and time?
提问by mcbeav
I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:
我将日期存储在 mysql 表中,它们被设置为在每一行中存储为 CURRENT TIMESTAMP 并存储如下:
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:
但我想做的是以某种方式使用 PHP 来分离所有内容并创建一个更像的日期:
May 29 2010 1:17 AM
2010 年 5 月 29 日上午 1:17
can anyone at least direct me in the right path that i should take. any help is greatly appreciated!
任何人都可以至少指导我走正确的道路,我应该走。任何帮助是极大的赞赏!
回答by deceze
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
回答by Pascal MARTIN
You have two solutions :
您有两种解决方案:
- Use
strtotime()
to parse the date to a timestamp, anddate()
to re-format it to a string - Or use the
DateTime
class
- 用于
strtotime()
将日期解析为时间戳,并将date()
其重新格式化为字符串 - 或者使用
DateTime
类
In PHP code, this would mean using :
在 PHP 代码中,这意味着使用:
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).
strtotime
+date
是您最常使用的解决方案;但这不是最好的解决方案:有了这些,您将使用 UNIX 时间戳,这意味着日期范围有限(从 1970 年到 2038 年,如果使用 32 位整数)。
ON the other hand, using the DateTime
class, there will be no limit to the range of dates you can work with.
另一方面,使用DateTime
该类,您可以使用的日期范围将没有限制。
回答by sreekanth
If you have a DATETIME
field in your table and you only want the date field, then:
如果您DATETIME
的表中有一个字段并且您只想要日期字段,则:
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.
timestamp
你的列名在哪里。