duanmie9682 2015-02-05 08:00 采纳率: 100%
浏览 82
已采纳

在时间格式之间转换语法

just having a little trouble converting between php time formats... not sure where I am off. Amongst other things, I am changing between 12 and 24 hour time.

$start_time= "02/12/2015 12:00 AM";  //this is a string

$myDateTime = DateTime::createFromFormat('m/d/Y h:i a', $start_time);
$newDateString = $myDateTime->format('Y-m-d H:i');

echo ($newDateString);  //empty at the moment

My desired output format is:

2015-02-12 12:00

Sincere thanks for any help. It is greatly appreciated!

  • 写回答

1条回答 默认 最新

  • duan0065626385 2015-02-05 08:07
    关注

    There is nothing wrong with your code except a missing ; in the first line. Only one small change you can make. In the output you can change H to h since your desired output is in 12 hour format, rest is already fine.

    $newDateString = $myDateTime->format('Y-m-d h:i');
    

    2015-02-12 12:00

    Fiddle

    P.S: Sometimes i seriously wonder some up-votes were not necessary at all, I mean you could have run this code yourself and noticed it was already good. Just gave some up-votes to someone without taking any real help from them.

    My Act of telling you that you are missing a ; and need to change one letter is not a real help. Waste of time for you too :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?