简体   繁体   English

将时间转换为PHP中的另一种格式时出错

[英]Error when converting time to another format in PHP

I'm trying to change the time format here. 我正在尝试在此处更改时间格式。

In the database, the time are saved as '0','1200','1300','1400','900', etc. 在数据库中,时间保存为“ 0”,“ 1200”,“ 1300”,“ 1400”,“ 900”等。

I want them to have a normal format like this: '0:00 AM', '12:00 PM', '1:00 PM', '2:00 PM', '9:00 AM'. 我希望他们使用这样的常规格式:“ 0:00 AM”,“ 12:00 PM”,“ 1:00 PM”,“ 2:00 PM”,“ 9:00 AM”。

In the code I'm using : 在我使用的代码中:

$timeFormated = DateTime::createFromFormat('Gi', $myTime[$i]);
$newDateString = $timeFormated->format('g:i A'); 

However, I'm getting the error saying “Call to a member function format() on a non-object”. 但是,我收到错误消息:“在非对象上调用成员函数format()”。

Can anyone tell me what I did wrong here? 谁能告诉我我在这里做错了什么? Thanks! 谢谢!

It should work if you ensure your times are 4 digits. 如果您确保自己的时间是4位数,则应该可以使用。

You can do this by using: 您可以使用以下方法完成此操作:

$myTime = sprintf('%04d', $myTime);

So, 所以,

$timeFormated = DateTime::createFromFormat('Gi', '0'); // FALSE - doesn't work
$timeFormated = DateTime::createFromFormat('Gi', '0000'); // Works..!

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

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