如何将此日期转换为 Ymd (PHP)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10478573/
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
How to convert this date to Y-m-d (PHP)?
提问by EOB
I have this date as a string: 10.21.2011, the format is mm.dd.yyyy
我将此日期作为字符串:10.21.2011,格式为 mm.dd.yyyy
How can I convert this to Y-m-d? This is what I tried, but it always gives me the 1970 date.
如何将其转换为 Ymd?这是我尝试过的,但它总是给我 1970 年的日期。
$date = date('Y-m-d', strtotime($date));
Thanks!
谢谢!
采纳答案by Sarfraz
Most likely because of period .:
很可能是因为期间.:
$date = '10.21.2011';
echo date('Y-m-d', strtotime(str_replace('.', '/', $date)));
Result:
结果:
2011-10-21
回答by eis
The object-oriented way:
面向对象的方法:
$date = DateTime::createFromFormat('m.j.Y', '10.21.2011');
echo $date->format('Y-m-d');
My opinion is that this is the approach one should try to learn with PHP.
我的观点是,这是人们应该尝试学习 PHP 的方法。
Requires PHP 5.3.
需要 PHP 5.3。
回答by Falcon
$date = '10.21.2011'; $date = strtotime(str_replace('.','/',$date)); echo date("Y-m-d",$date);
$date = '10.21.2011'; $date = strtotime(str_replace('.','/',$date)); echo date("Y-m-d",$date);
This should do it
这应该做
回答by ndlinh
$date = preg_replace('/(\d{2})\.(\d{2})\.(\d{4})/', '--', $date);

