-2

我有一系列小时/分钟,

$array = array("09:00","09:05","09:10","09:15","09:20","09:25","09:30","09:35","09:40","09:45","09:50","09:55","10:00","10:05","10:10","10:15","10:20","10:25","10:30","10:35","10:40","10:45","10:50","10:55",15:00","15:05","15:10","15:15","15:20","15:25","15:30","15:35","15:40","15:45","  15:50","15:55","16:00","16:05","16:10","16:15","16:20","16:25","16:30","16:35","16:40","16:45","16:50","16:55");    

如何添加缺少的时间?(从 11:00 到 14:55)这些元素彼此之间相差 5 分钟。

4

2 回答 2

0

我以为人们会急于回答-我的评论是站着的,但是您将所有这些时间都存储在一个数组中似乎有点过头了,但要回答这个问题……不一定是最简单的方法,但最容易阅读的方法是使用 PHP 的日期函数:

for($i=strtotime('11:00'),$j=strtotime('15:00');$i<$j;$i+=5*60)
    $array[] = gmdate('H:i', $i);
$array = array_unique($array);
asort($array);

print_r($array);

运行代码

于 2013-05-03T11:20:42.287 回答
0

首先,它应该是:

$array = array("09:00",...);

其次,您可以只生成整个数组。如果您绝对必须这样做,那么最简单的方法是遍历您拥有的数组并将缺少的项目添加到新数组中,然后合并它们并对结果数组进行排序。

于 2013-05-03T10:20:32.133 回答