我有一个像下面这样的日期时间
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 PMIt 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
更多推荐
发布评论