0

Current Time format:

 04:16:16

Now i want it to divide in 4 intervals as shown below.

$time1 = 01:04:04
$time2 = 02:08:08  
$time3 = 03:12:12
$time4 = 04:16:16
Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41
  • 1
    Have you tried something ? – Rizier123 Jul 16 '15 at 13:34
  • [Convert H:i:s format to seconds](http://stackoverflow.com/a/20874702/67332), do some easy math, and then [convert seconds back to H:i:s format](http://stackoverflow.com/a/20870843/67332). – Glavić Jul 16 '15 at 16:41

1 Answers1

3

Looks like you've tried nothing and are already out of ideas.
Since it's simple and I'm bored enough:

// 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);

output

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

output

[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
Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41
spenibus
  • 4,339
  • 11
  • 26
  • 35