本问题已经有最佳答案,请猛点这里访问。
编辑:
我需要将这个日期时间从dd/mm/yy H:i:s格式改为Y-m-d H:i:s格式。
这是针对PHP或CodeIgniter项目的。
我从其他报告中得到一些数据格式不好。
datetime
7/12/17 12:15:23 pm (dd/mm/yy H:i:s) format
8/12/17 5:18:12 am
20/12/17 5:12:24 pm
21/12/17 12:17:37 pm
如果我得到使用第一格式日期的示例。
$example_date ="7/12/17 12:15:23 pm"; // d F Y : 07-December-2017 12:15:23
$format_date = $date("Y-m-d", strtotime($example_date));
印刷体:2007-12-17 12:15:23是错误的结果。
在d F Y日,它打印了17 December 2007号,但它应该是07 December 2017号。
通常我们可以这样更改日期:
$example_datetime ="13/12/2017 12:08:16 pm"; // (dd/mm/yy H:i:s) format
$format_date = date("Y-m-d H:i:s", strtotime($example_datetime));
Result from echo $format_date => 2017-12-13 12:08:16 is correct. If the year is 2017 and this is what I wanted to.
"当我在(Y M D"H:":(S,strtotime datetime becomes美元);EN:07—12—17—23 12:15:"废话,that is not with the given specifier甚至可能的格式。加,3v4l.org / jaoga proves this not to be the房屋。我知道,我的实际问题是什么?请编辑你的提问,包括安布尔that shows the executable示例问题。
嗨,that is(DD /格式YY)。我知道that must be日期7,12月或12月must be,or must be 2017年17岁。那你给我的链接,其生产日期7月12 As和As。
我知道你的问题是关于日期的字符串使用strtotime parsing given them,and not about输出使用的日期。
我想ouputing using them to date,编辑我的问题…………………11对不起confusing for。
问题edited。Please检查。
this is not of the问题仍然在输出数据。naincy tried to the解释你的问题的答案ramraider' is already,and shows how to You avoid恩,告诉你什么好的php格式输入日期是预付的。
您可以使用DateTime和createFromFormat方法—指定输入日期的格式,然后格式化您希望输出的格式—例如:
$example_datetime ="13/12/2017 12:08:16 pm";
$date=DateTime::createFromFormat('d/m/Y H:i:s a', $example_datetime );
$output=$date->format('Y-m-d H:i:s');
echo $output; // -> 2017-12-13 12:08:16
对于输入日期戳中的2位年份,而不是大写的Y,小写的Y应该匹配。
$example_datetime ="13/12/17 12:08:16 pm";
$date=DateTime::createFromFormat('d/m/y H:i:s a', $example_datetime );
$output=$date->format('Y-m-d H:i:s');
echo $output; // -> 2017-12-13 12:08:16
你能试着用:13/12/17 12:08:16 pm作为输出吗?谢谢您。
EDOCX1?2??上述输出是否如预期,因此我不理解您的评论?
已编辑问题。请再检查一下。谢谢您。
@zinc如果要用2位年份而不是4位年份来分析日期,则需要相应地替换格式字符串中指定年份的部分。
@我会再试一次,谢谢你的帮助。我从Ramraider那里找到了正确的答案。我编辑了他的答案。批准后我会改正的。
试试这个
$example_datetime ="13/12/2017 12:08:16 pm"; // (dd/mm/yy H:i:s) format
$date = str_replace('/', '-', $example_datetime);
echo date("Y-m-d H:i:s", strtotime($date)); // Y-m-d H:i:s
Dates in the m/d/y or d-m-y formats are disambiguate by looking at the
separator between the various components: if the separator is a slash
(/), then the American m/d/y is assumed; whereas if the separator is a
dash (-) or a dot (.), then the European d-m-y format is assumed.
Check more here: http://php.net/manual/en/function.strtotime.php
你好,Naincy,谢谢你的帮助。但是你能不能试着用13/12/17 12:08:16 pm作为日期?
问题已编辑。请检查