简体   繁体   English

如何以给定的分钟数增加给定时间并将新时间与数组列表相关联

[英]How to increment a given time with a given number of minutes and associate the new time to a array list

I need to increment a given time with a given minutes number and and associate the time with an array list of id's. 我需要用给定的分钟数增加给定的时间,并将该时间与ID的数组列表相关联。 An example of what I need : 我需要的一个例子:

$ids_arrays = array(699926900040821, 699926900040822, 699926900040823); 
$given_time='20:30'; 
$given_minutes = '5';
$newarray=array(); 

And I want to create a new array like this : 我想创建一个像这样的新数组:

Array (
        [699926900040821] => '20:35'
        [699926900040822] => '20:40'
        [699926900040822] => '20:45' 
      )

My code : 我的代码:

//$_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");
}

Try this: 尝试这个:

$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);
}

This would do: 这样可以:

/**
 * @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;
}

Then you'd call it like this: 然后,您可以这样称呼它:

$associatedIds = associateIdsWithTime(
    array(699926900040821, 699926900040822, 699926900040823),
    DateTime::createFromFormat('H:i', $given_time),
    DateInterval::createFromDateString("+ $given_minutes minutes")
);

For $given_time = 20:30 and $given_minutes = 5 it will return: 对于$given_time = 20:30$given_minutes = 5 ,它将返回:

Array
(
    [699926900040821] => 20:35
    [699926900040822] => 20:40
    [699926900040823] => 20:45
)

On a side note: take caution with your ids on 32bit systems. 附带一提:请注意32位系统上的ID。 You are using integers but PHP_INT_MAX on 32bit systems is limited to 2.147.483.647, which cannot fit your 699.926.900.040.822. 您使用的是整数,但32位系统上的PHP_INT_MAX限制为2.147.483.647,不能容纳699.926.900.040.822。 You might want to change to strings to keep it portable. 您可能需要更改为字符串以使其可移植。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取从现在到 MySQL 数据库中给定的日期时间的天数、小时数、分钟数和秒数? - How can I get number of days, hours, minutes and second from now to a date-time given in the MySQL database? 如何递减给定的时间 - How to decrement given time 使用开始时间和结束时间制定给定分钟的时间表 - making schedule of given minutes using start time and end time 通过比较当前时间和给定时间,将日期增加 1 天 - Increment the date by 1 day by comparing the current time and given time 如何确定给定数字是否在给定数组范围内? - How to identify if given number is there in a given array range? 如何在WordPress中显示给定时间范围内的帖子数 - How to display number of posts for a given time frame in wordpress 使用AM / PM选择列表增加时间15分钟乘12小时 - Select list increment time by 15 minutes by 12 hour with AM/PM 如何减少1秒到给定的时间 - How to decrease 1 second to a given time 给定1000个字符串数组,每个字符串具有关联的时间间隔值X,我如何每x分钟对每个字符串执行一项任务? - Given array of 1000 strings wih an associated time-interval value X for each, how do I perform a task for each of the strings, every x minutes? 给定一系列不同持续时间的对象,如何在给定的时间范围内找到所有可能的安排 - Given an array of objects of varying durations, how can you find all possible arrangements within a given time frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM