我需要使用给定的分钟数增加给定时间,并将时间与id的数组列表相关联。我需要的一个例子:
$ids_arrays = array(699926900040821, 699926900040822, 699926900040823);
$given_time='20:30';
$given_minutes = '5';
$newarray=array();
我想创建一个像这样的新数组:
Array (
[699926900040821] => '20:35'
[699926900040822] => '20:40'
[699926900040822] => '20:45'
)
我的代码:
//$_GET['grupuri']= simple array ,$ids_arrays;
//$_GET['numar_grup']= number of minutes to increment ;
//$_GET['time_grup']=time array;
$ora_grup=array();
$h1=new DateTime($_GET['time_grup']);
$m1=new DateInterval('PT'.$_GET['numar_grup'].'M');
for ( $i=0; $h=$h1; $h->format('H') <10; $i <count($_GET['grupuri']) ; $i++, $h->add($m1)) {
$ora_grup[$_GET['grupuri'][$i]]=$h->format("H:i\n");
}
答案 0 :(得分:2)
试试这个:
$ids_arrays = array('699926900040821', '699926900040822', '699926900040823');
$given_time = '20:30';
$given_minutes = '5';
$t = strtotime('today '. $given_time);
$newarray = array();
foreach ($ids_arrays as $id) {
$newarray[$id] = date('H:i:s', $t += $given_minutes * 60);
}
答案 1 :(得分:2)
这样做:
/**
* @return array
*/
function associateIdsWithTime(
array $ids,
DateTime $startTime,
DateInterval $interval
) {
$associatedIds = array();
foreach ($ids as $id) {
$associatedIds[$id] = $startTime->add($interval)->format('H:i');
}
return $associatedIds;
}
然后你就这样称呼它:
$associatedIds = associateIdsWithTime(
array(699926900040821, 699926900040822, 699926900040823),
DateTime::createFromFormat('H:i', $given_time),
DateInterval::createFromDateString("+ $given_minutes minutes")
);
对于$given_time = 20:30
和$given_minutes = 5
,它将返回:
Array
(
[699926900040821] => 20:35
[699926900040822] => 20:40
[699926900040823] => 20:45
)
旁注:请注意32位系统上的ID。您使用的是整数,但32位系统上的PHP_INT_MAX
限制为2.147.483.647,这不适合您的699.926.900.040.822。您可能希望更改为字符串以使其可移植。