0

I'm running into a discrepancy with either my conversion of an integer into defined Minutes.

<?php
$seconds = 269;
echo date("G:i:s", $seconds);
// result: 0:04:29
?>

I figured I'd double check on some sites to see if the conversion is correct:

Here's one I found: http://www.thecalculatorsite.com/conversions/time.php

The result returned is: 4.4833333333333

Example 1 returns: 0:04:29 Example 2 returns: 4.4833333333333

I'm confused about this. What am I missing here. Am I using the date() function incorrectly?

coffeemonitor
  • 12,780
  • 34
  • 99
  • 149

2 Answers2

0

Be careful with date(). it expects to be provided with a PHP timestamp, which is number of seconds since midnight, Jan 1/1970. It will "work" for small timestamp values, but will get increasingly wrong as you pass in "larger" timestamps, as you will be dealing with months/days/years in 1970, plus leapyears, etc...

As for the conversion, what's wrong with it? 4.48333... is 4 full minutes, and 0.483333 is simply 29/60.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

You can use DateTime class for time calculation:

Code:

$start = new DateTime;
$end = clone $start;
$end->modify('+269 seconds');
$diff = $start->diff($end);
print_r($diff);

Output:

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 4
    [s] => 29
    [invert] => 0
    [days] => 0
)

As you can see in output, you have all the info you need. To access it, just use $diff->i for minutes, $diff->s for seconds etc.

Glavić
  • 42,781
  • 13
  • 77
  • 107