在PHP中将api返回日期时间字符串从24格式转换为12小时格式(convert the api return datetime string from 24 format to 12 hour fo

编程入门 行业动态 更新时间:2024-10-28 18:22:53
在PHP中将api返回日期时间字符串从24格式转换为12小时格式(convert the api return datetime string from 24 format to 12 hour format in php)

我有一个像下面这样的日期时间

17/05/2018 23:59:00 PM

它像varchar(100)那样存储在数据库中。 我想将此转换为17/05/2018 11:59:00 PM。 它从api返回。我试图修复尝试了很多方法。 不知何故,它正在工作,但即将到来的明天约会像现在这样给予

18/05/2018 11:59 AM

。有任何有效的方法来做到这一点。

$startdate='17/05/2018 23:59:00 PM'; $myDateTime = DateTime::createFromFormat('d/m/Y H:i A',$startdate); $newDateString = $myDateTime->format('d/m/Y h:i A'); echo $newDateString;

I have a datetime like below

17/05/2018 23:59:00 PM

It is storing in database like varchar(100). I want to convert this as 17/05/2018 11:59:00 PM. It is returning from the api.I have tried to fix tried so many ways. somehow it is working but coming tomorrow date like it is giving now like this

18/05/2018 11:59 AM

.Is any efficient way to do this.

$startdate='17/05/2018 23:59:00 PM'; $myDateTime = DateTime::createFromFormat('d/m/Y H:i A',$startdate); $newDateString = $myDateTime->format('d/m/Y h:i A'); echo $newDateString;

最满意答案

您正在使用AM / PM和24小时格式。 这没有意义。 PHP可能正在尽最大努力去理解它,并在日期时间中增加12个小时将它推到第二天。

如果你忽略你的日期时间PM,这工作得很好:

$startdate='17/05/2018 23:59:00 PM'; $myDateTime = DateTime::createFromFormat('d/m/Y H:i:s+',$startdate); $newDateString = $myDateTime->format('d/m/Y h:i:s A'); echo $newDateString;

演示

You are using AM/PM and 24 hour format. That doesn't make sense. PHP is probably trying its best to make sense of it and adding 12 hours to the datetime pushing it to the next day.

If you ignore the PM from your datetime this works just fine:

$startdate='17/05/2018 23:59:00 PM'; $myDateTime = DateTime::createFromFormat('d/m/Y H:i:s+',$startdate); $newDateString = $myDateTime->format('d/m/Y h:i:s A'); echo $newDateString;

Demo

更多推荐

本文发布于:2023-08-05 22:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   转换为   字符串   中将   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!