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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:52:42  来源:igfitidea点击:

Convert this string to datetime

php

提问by emeraldhieu

Possible Duplicate:
PHP: Convert uncommon date format to timestamp in most efficient manner possible?

可能的重复:
PHP:以最有效的方式将不常见的日期格式转换为时间戳?

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

使用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:02and 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:02date('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);