简体   繁体   English

如何使用 PHP 将此日期:“16/11/2020 12:00:00 am”转换为“2020-11-16 00:00:00”?

[英]How convert this date: "16/11/2020 12:00:00 a. m." to "2020-11-16 00:00:00" with PHP?

Currently, I am requesting an external web service to get some important information.目前,我正在请求外部 web 服务以获取一些重要信息。 However, the dates this web services return me are something like this:然而,这个 web 服务返回给我的日期是这样的:

16/11/2020 12:00:00 am 16/11/2020 12:00:00 上午

So, I was in other implementations using a code like this to get the DateTime:所以,我在其他实现中使用这样的代码来获取日期时间:

$date = '16/11/2020 12:00:00 a. m.';
$newDateTime = new \DateTime($date);

But it throws this error:但它抛出了这个错误:

DateTime::__construct(): Failed to parse time string (16/11/2020 12:00:00 am) at position 0 (1): Unexpected character DateTime::__construct():无法在 position 0 (1) 解析时间字符串 (16/11/2020 12:00:00 am):意外字符

At first, I thought the "am" part could cause it to I did this change:起初,我认为“am”部分可能会导致我做了这个改变:

$date = '16/11/2020 12:00:00 a. m.';
$date        =  str_replace('a. m.', 'am', $date);
$date        =  str_replace('p. m.', 'pm', $date);
$newDateTime = new \DateTime($date);

But the error persists.但错误仍然存在。 My last try was this:我最后一次尝试是这样的:

$date = '16/11/2020 12:00:00 a. m.';
$date        =  str_replace('a. m.', 'am', $date);
$date        =  str_replace('p. m.', 'pm', $date);
$date        =  date("Y-m-d H:s:i", strtotime($date));
$newDateTime = new \DateTime($date);

But the result date I got is:但我得到的结果日期是:

1970-01-01 00:00:00 1970-01-01 00:00:00

So, what is the correct form to parse this kind of date?那么,解析这种日期的正确形式是什么?

Thanks.谢谢。

You need to provide a matching format description:您需要提供匹配的格式描述:

<?php
$date = '16/11/2020 12:05:30 a. m.';
$date        =  str_replace('a. m.', 'am', $date);
$date        =  str_replace('p. m.', 'pm', $date);
$newDateTime = \DateTime::createFromFormat('d/m/Y h:i:s A', $date);
print_r($newDateTime->format('Y-m-d h:i:s'));

The output is: output 是:

2020-11-16 12:05:30 2020-11-16 12:05:30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以这种格式“ 2013-11-16T12:30:00 + 02:00”在PHP中生成日期时间戳? - How to generate a date timestamp in PHP in this format “2013-11-16T12:30:00+02:00”? Laravel每小时08:00、12:00、16:00和20:00、22:00运行任务 - Laravel run task every hour on 08:00, 12:00, 16:00 and 20:00, 22:00 为什么0000-00-00值将日期%d /%m /%Y转换为30/11 / -1? - Why 0000-00-00 value convert to 30/11/-1 with date %d/%m/%Y? 2020 年 11 月 1 日星期日 00:00:00 GMT+0530(印度标准时间)到 PHP 中的 Ymd - Sun Nov 01 2020 00:00:00 GMT+0530 (India Standard Time) to Y-m-d in PHP php将12hr转换为24hr 12:00:am-&gt; 00:00:00 - php convert 12hr to 24hr 12:00:am -> 00:00:00 如何在PHP中将“ 2012-10-16T14:46:33 + 00:00”转换为Unix Timestamp? - How to convert “2012-10-16T14:46:33+00:00” into Unix Timestamp in PHP? 订单创建时间为 00:00:00 至 11:59:00 和日期 - Order by created at with time 00:00:00 until 11:59:00 and date 如何使用PHP将2010-07-28 00:00:00转换为2010年7月28日00:00:00 - How to convert 2010-07-28 00:00:00 to July 28, 2010, 00:00:00 using PHP 如何防止 PHP 将具有 0000-00-00 值的 DateTime object 转换为 -0001-11-30 - How to prevent PHP to convert a DateTime object with 0000-00-00 value to -0001-11-30 MySQL 日期:0000-00-00 00:00:00 - MySQL date: 0000-00-00 00:00:00
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM