PHP DateTime :: createFromFormat在输出格式为g:ma时添加12分钟

时间:2017-12-20 20:29:24

标签: php datetime datetime-format

我正在尝试将24小时分割/转换为12小时,但是PHP似乎在输出中添加了12分钟或1分钟,并且在尝试时区和系统时间修复后我无法确定原因。你能教育我吗?

输入:

<?php $timeSlot = ["10:00","22:00"];
    echo DateTime::createFromFormat('H:i',$timeSlot[0])->format('H:i');
    echo " - ";
    echo DateTime::createFromFormat('H:i',$timeSlot[1])->format('H:i');
    echo " Fine.<br>";
    echo DateTime::createFromFormat('H:i',$timeSlot[0])->format('g:ma');
    echo " - ";
    echo DateTime::createFromFormat('H:i',$timeSlot[1],new DateTimeZone('Australia/Sydney'))->format('g:ma');
    echo " Why 12 extra minutes?<br>";
    echo DateTime::createFromFormat('!H:i',$timeSlot[0])->format('g:ma');
    echo " - ";
    echo DateTime::createFromFormat('!H:i',$timeSlot[1],new DateTimeZone('Australia/Sydney'))->format('g:ma'); 
    echo " Why one extra minute?"; ?>

输出:

10:00 - 22:00 Fine.
10:12am - 10:12pm Why 12 extra minutes?
10:01am - 10:01pm Why one extra minute?

1 个答案:

答案 0 :(得分:3)

你没有得到额外的分钟。您正在显示该月的数字(12月12日)。将格式更改为g:ia

当您执行!H:i时,您将设置1970年1月1日的Unix纪元的月,日和年。因此,您在这些设置中看到1月的01

见下表: http://php.net/manual/en/function.date.php