设备H:i:s格式为4等于inverval

时间:2015-07-16 13:31:40

标签: php datetime time

当前时间格式:

 04:16:16

现在我希望它分为4个区间,如下所示。

$time1 = 01:04:04
$time2 = 02:08:08  
$time3 = 03:12:12
$time4 = 04:16:16

1 个答案:

答案 0 :(得分:3)

看起来你什么都没做,而且已经没有想法了 因为它很简单,我很无聊:

// This should work fine up to 298261:37:04 (1073741824 seconds, aka 2^30)

function makeIntervals($t, $divide=4) {

   // get individual values
   $t = explode(':', $t);

   // turn into seconds
   $t = $t[0] * 3600 // hours
      + $t[1] * 60   // minutes
      + $t[1];       // seconds

   // interval separation value
   $t = $t/$divide;

   // init output array
   $time = array();

   // build output
   for($i=1; $i<=$divide; ++$i) {
      $currentTime = $i * $t;
      $time[] = sprintf(
         '%02d:%02d:%02d',
         floor($currentTime/3600),
         floor($currentTime%3600/60),
         $currentTime%60
      );
   }

   return $time;
}

$intervals = makeIntervals('04:16:16');
$intervals = makeIntervals('04:16:16', 16);

输出

[0] => 01:04:04
[1] => 02:08:08
[2] => 03:12:12
[3] => 04:16:16

输出

[0] => 00:16:01
[1] => 01:32:02
[2] => 01:48:03
[3] => 01:04:04
[4] => 01:20:05
[5] => 02:36:06
[6] => 02:52:07
[7] => 02:08:08
[8] => 02:24:09
[9] => 03:40:10
[10] => 03:56:11
[11] => 03:12:12
[12] => 03:28:13
[13] => 04:44:14
[14] => 04:00:15
[15] => 04:16:16