简体   繁体   English

PHP:时间增量

[英]PHP: time increment

In my PHP code I have an array1 that has 2 columns, ie: 在我的PHP代码中,我有一个包含2列的array1 ,即:

ID | startDate
1  | 2012-02-03 11:50
2  | 2012-02-03 11:50
3  | 2012-02-03 11:50
4  | 2012-02-03 11:50

I need to transform array1 into the following array2 : 我需要将array1转换为以下array2

ID | startDate
1  | 2012-02-03 11:50:00
2  | 2012-02-03 11:50:30
3  | 2012-02-03 11:51:00
4  | 2012-02-03 11:51:30

So, I should just increment a time by 30 seconds starting from the second entry. 所以,我应该从第二个条目开始增加30秒的时间。 This increment should be cumulatively propagated as you may see in example. 如您在示例中所示,此增量应累积传播。 I'm not very fluent in PHP. 我的PHP不是很流利。 Does anybody know time-related PHP functions that could do this job in few lines of code? 有没有人知道与时间相关的PHP函数可以在几行代码中完成这项工作?

I would look at date() and strtotime() 我会看看date()strtotime()

$array2 = array();
$incr = 0;
foreach ($array1 as $value) {
    $time = strtotime($value);
    $array2[] = date('Y-m-d H:i:s', $time + $incr);
    $incr += 30; 
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM