从不需要的时候删除 :00

Drop the :00 from the time when it's not necessary

我目前拥有的是从 23:54(24 小时)格式的数据库记录中提取的时间范围。 我下面的脚本当前将时间范围格式化为:6:30am - 1:00pm 但我希望它做的是将 :00 删除任何 "on the hour" 次,因此它看起来像:6:30am - 下午 1 点。

我知道这听起来可能微不足道,但最终目标是能够像这样始终格式化,以便在下午 1 点到 6 点时看起来更整洁。 :-)

<?php
$daystarttime = date("g:ia", strtotime($record['daystarttime']));
echo $daystarttime;
$dayendtime = date("g:ia", strtotime($record['dayendtime']));

if (!$dayendtime == null) {
    echo " - ";
    echo $dayendtime;
}
?>

非常感谢您的帮助。 :-)

使用str_replace。如果没有':00'次,则什么都不替换。

$daystarttime = str_replace(':00', '', date("g:ia", strtotime($record['daystarttime'])));
$dayendtime = str_replace(':00', '', date("g:ia", strtotime($record['dayendtime'])));