如何在PHP中将24小时制的时间转换为12小时制的时间,例如1630应该是下午4:30。
有文章介绍如何将24小时格式的时间转换为12小时格式的时间,例如16:30转换为04:30 PM,但我需要将小时转换为时间。
我现在是通过将时间除以1200并获得余数作为分钟来完成的,但我希望能够使用日期或时间函数另外实现。
我认为这对你来说会有效。
date("h:i a",strtotime("1630"));
<?php
$date = date("Y-m-d 16:30:00");
echo date("h:i a",strtotime($date));
?>
演示链接: https://eval.in/858486
或者如果您的时间是1630
$time = 1630;
$time = (int)(1630/100).":".(1630%100);
$date = date("Y-m-d $time:00");
echo date("h:i a",strtotime($date));
更新 3 位数,如920
或815
$time = "0920"; // here $time should be string `0` is for other base of number
$last2 = substr($time, -2);
$time = str_replace($last2,":".$last2,$time);
$date = date("Y-m-d $time");
echo date("h:i a",strtotime($date));
0900, 0800
。 - Niklesh Raut