douzhang8144 2016-06-27 13:57
浏览 13
已采纳

将未格式化的时间编号格式化为24小时可读

My client has entered some start and end dates into our system and I want to be able to format it with PHP.

The values are 930 and 1530. Processing the 1530 variable is fine but its the 930 that returns false.

Here is my script so far but no success. Error returns bool(false) because it can't get a readable time (I believe?)

    $time = DateTime::createFromFormat('Hi', $var);
    $format = "H:i";
    $result = $time->format($format);
  • 写回答

2条回答 默认 最新

  • dputlf5431 2016-06-27 14:04
    关注

    That's because your initial time format is ambiguous. Assuming that you have time without leading zeros all the time one can do something like this:

    $var = '930';
    $time = DateTime::createFromFormat('Hi', str_pad($var, 4, '0', STR_PAD_LEFT));
    $format = "H:i";
    $result = $time->format($format);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?