使用 PHP 将时间戳转换为正常日期格式和反向转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7222726/
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
Timestamp to normal date format and reverse conversion using PHP
提问by blasteralfred Ψ
I have a timestamp as 2011-08-27 18:29:31
. I want to convert it to 27 Aug 2011 06.29.31 PM
. Also, I want to convert this format reverse back to the previous timestamp format.
How van I do this using PHP?
我有一个时间戳为2011-08-27 18:29:31
. 我想将其转换为27 Aug 2011 06.29.31 PM
. 另外,我想将此格式反向转换回以前的时间戳格式。我如何使用 PHP 做到这一点?
回答by Patryk Grandt
$converted = date('d M Y h.i.s A', strtotime('2011-08-27 18:29:31'));
$reversed = date('Y-m-d H.i.s', strtotime($converted));
回答by madeinukraine
Don't use date()! It`s too old function. In PHP v. 5.2 and more you should use date_format or DateTime::format object.
不要使用日期()!功能太老了。在 PHP v. 5.2 及更高版本中,您应该使用date_format 或 DateTime::format 对象。
回答by Brian Glaz
you can use the date_format()
function
你可以使用这个date_format()
功能
//Convert to format: 27 Aug 2011 06.29.31 PM
$converted_date = date_format('d M Y h.i.s A',strtotime($orig_date));
//Convert to format 2011-08-27 18:29:31
$converted_date = date_format('Y-m-d H:i:s',strtotime($orig_date));
回答by rajasaur
回答by Bez Hermoso
To convert from 2011-08-27 18:29:31
to 27 Aug 2011 06.29.31 PM
:
转换2011-08-27 18:29:31
为27 Aug 2011 06.29.31 PM
:
echo date('d M Y, H.i.s A', strtotime('2011-08-27 18:29:31'));
To do the reverse:
反过来做:
echo date('Y-m-d H:i:s',strtotime('27 Aug 2011 06.29.31 PM'));
If that doesn't work, you may have to try:
如果这不起作用,您可能必须尝试:
$date = date_create_from_format('d M Y, H.i.s A', '27 Aug 2011 06.29.31 PM');
echo date_format("Y-m-d H:i:s",$date);