简体   繁体   English

PHP:将日期格式 d/m/Y h:i:sa 更改为 m/d/Y h:i:sa

[英]PHP: Change Date format d/m/Y h:i:s a to m/d/Y h:i:s a

Example Dates:示例日期:

31/10/2019 7:15 am
31/10/2019 11:58 am

Output:输出:

01-01-1970 08:00:00 am
01-01-1970 08:00:00 am

I'm trying to change the date format on my database.我正在尝试更改数据库中的日期格式。
I've already try date("dmY h:i:sa", strtotime("31/10/2019 7:15 am")) but it shows the output above.我已经尝试过date("dmY h:i:sa", strtotime("31/10/2019 7:15 am"))但它显示了上面的输出。

Please help.请帮忙。

You can use DateTime::createFromFormat to parse the original date, and then format it in the way you want using format .您可以使用DateTime::createFromFormat来解析原始日期,然后使用format以您想要的方式对其进行format

function reformatDate ($dateStr) {
  return DateTime::createFromFormat('d/m/Y h:i a', $dateStr)->format('m/d/Y h:i:s a');
}

echo reformatDate('31/10/2019 7:15 am') . "\n"; // 10/31/2019 07:15:00 am
echo reformatDate('31/10/2019 11:58 am') . "\n"; // 10/31/2019 11:58:00 am

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM