php 将此字符串转换为日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8063057/
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 this string to datetime
提问by emeraldhieu
Possible Duplicate:
PHP: Convert uncommon date format to timestamp in most efficient manner possible?
How to convert this string to datetime format? 06/Oct/2011:19:00:02
如何将此字符串转换为日期时间格式?06/Oct/2011:19:00:02
I tried this but it doesn't work.
我试过这个,但它不起作用。
$s = '06/Oct/2011:19:00:02';
$date = strtotime($s);
echo date('d/M/Y:H:i:s', $date);
回答by Criss
Use DateTime::createFromFormat
$date = date_create_from_format('d/m/Y:H:i:s', $s);
$date->getTimestamp();
回答by NBK
The Problem is with your code formatting,
问题在于您的代码格式,
inorder to use strtotime()
You should replace '06/Oct/2011:19:00:02'
with 06/10/2011 19:00:02
and date('d/M/Y:H:i:s', $date);
with date('d/M/Y H:i:s', $date);
. Note the spaces in between.
序使用strtotime()
,就应该替换'06/Oct/2011:19:00:02'
用06/10/2011 19:00:02
和date('d/M/Y:H:i:s', $date);
用date('d/M/Y H:i:s', $date);
。注意中间的空格。
So the final code looks like this
所以最终的代码看起来像这样
$s = '06/10/2011 19:00:02';
$date = strtotime($s);
echo date('d/M/Y H:i:s', $date);