Kieron606
Kieron606

Reputation: 613

Add epoch time with milliseconds to a epoch date/time?

I have a function in PHP which gets a mean time of epoch dates in an array

Intervals - Array
(
  [0] => 86340
  [1] => 86400
  [2] => 86400
  [3] => 86400
  [4] => 86400
  [5] => 172800
  [6] => 86400
)

I am working the mean by adding all the elements in the array and dividing by the amount of elements in the array. My issue is sometimes it will return an epoch time like - '98734.285714286' rather than '98734'

Later on I am adding this mean time to an epoch date for example - '1453348800 + 98734.285714286'

But as the epoch date has a decimal place it seems to append the first 4 numbers after the decimal place to the epoch result for example - '1453354114.2857' and when this is converted back to a human readable date it is returning - 2903-01-22 05:28:34

Please note that these epoch dates are examples.

Upvotes: 1

Views: 177

Answers (2)

Erwin Moller
Erwin Moller

Reputation: 2408

They are integers, so treat them as integers.

$intervals = ['86340', '85340', '1453354114.2857'];
array_walk($intervals, 'intval'); // now they are all integer

ANd then do with it you want, like taking te avarage:

$avg = intval(array_sum($intervals)/count(intervals));

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

My issue is sometimes it will return an epoch time like - '98734.285714286' rather than '98734'

$average = intval($average); 

No explanation required :)

Upvotes: 1

Related Questions