Sylar
Sylar

Reputation: 357

Accurate seconds to "day / month / year - minutes : hours : seconds" conversion

I'm trying to convert an amount of seconds into a date. Here's what I currently have (in php):

function secondsToTime($inputSeconds) {
    $secondsInAMinute = 60;
    $secondsInAnHour  = 60 * $secondsInAMinute;
    $secondsInADay    = 24 * $secondsInAnHour;
    $secondsInAMonth = 30 * $secondsInADay;
    $secondsInAYear = 12 * $secondsInAMonth;

    $years = floor($inputSeconds / $secondsInAYear);

    $monthSeconds = $inputSeconds % $secondsInAYear;
    $months = floor($monthSeconds / $secondsInAMonth);

    $daySeconds = $monthSeconds % $secondsInAMonth;
    $days = floor($daySeconds / $secondsInADay);

    $hourSeconds = $daySeconds % $secondsInADay;
    $hours = floor($hourSeconds / $secondsInAnHour);

    $minuteSeconds = $hourSeconds % $secondsInAnHour;
    $minutes = floor($minuteSeconds / $secondsInAMinute);

    $remainingSeconds = $minuteSeconds % $secondsInAMinute;
    $seconds = ceil($remainingSeconds);

    $obj = array(
        'years' => (int) $years,
        'months' => (int) $months,
        'days' => (int) $days,
        'hours' => (int) $hours,
        'minutes' => (int) $minutes,
        'seconds' => (int) $seconds
    );
    return $obj;
}

but this isn't accurate enough, because it doesn't take into account the different length of the months...is there a known algorithm or something, to do it properly??

Thanks in advance

EDIT:

I'm sorry, english is not my language...I said I need to convert to a date, but what I'm actually doing is the difference between two dates, I get the result in seconds, and I need the amount of years, months, days, hours, minutes and seconds

Upvotes: 2

Views: 8952

Answers (5)

M_R_K
M_R_K

Reputation: 6350

I think this is what he is exactly ask for. I implemented this from jerdiggity answer. All credits goes to him.

This function actually converts given number of seconds to readable format.

(PHP > 5.3)

function seconds_in_redable($seconds) {
   $then = new DateTime(date('Y-m-d H:i:s', 0));
   $now = new DateTime(date('Y-m-d H:i:s', $seconds));
   $diff = $then->diff($now);
   return array('years' => $diff->y, 'months' => $diff->m, 'days' => $diff->d, 'hours' => $diff->h, 'minutes' => $diff->i, 'seconds' => $diff->s);
}

Upvotes: 2

Matt Murphy
Matt Murphy

Reputation: 330

I found jerdiggity's solution very helpful for my somewhat different problem. I needed to display the highest whole unit of time since creation of a row in human-readable format as in "Added {number} {correctly plural unit} ago." The only real change was conditionally assigning array keys based on plurality in order to just handle that out of the view. Done as a ZF2 helper, it became:

class PrettyDate extends AbstractHelper
{

    /**
     * Ultra-simple Human readable date to-string conversion
     *
     * @param date - epoch
     *
     */
    public function __invoke($date)
    {   
        foreach ($this->secondsToTime($date) as $unit=>$measure) {
            if ($measure) {
                return "Added $measure $unit ago.";
            }
        }
    }

    private function secondsToTime($inputSeconds) {
        $then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
        $now = new DateTime(date('Y-m-d H:i:s', time()));
        $diff = $then->diff($now);
        return array(
          ($diff->y>1?'years':'year') => $diff->y, 
          ($diff->m>1?'months':'month') => $diff->m, 
          ($diff->d>1?'days':'day') => $diff->d, 
          ($diff->h>1?'hours':'hour') => $diff->h,
          ($diff->i>1?'minutes':'minute') => $diff->i,
          ($diff->s>1?'seconds':'second') => $diff->s
        );
    }
}

Thanks, jerdiggity.

Upvotes: 1

jerdiggity
jerdiggity

Reputation: 3665

I think this is what you're looking for:

For PHP >= 5.3.0

function secondsToTime($inputSeconds) {
  $then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
  $now = new DateTime(date('Y-m-d H:i:s', time()));
  $diff = $then->diff($now);
  return array('years' => $diff->y, 'months' => $diff->m, 'days' => $diff->d, 'hours' => $diff->h, 'minutes' => $diff->i, 'seconds' => $diff->s);
}

For PHP >= 5.2.0

function secondsToTime($inputSeconds) {
  $then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
  $now = new DateTime(date('Y-m-d H:i:s', time()));
  $years_then = $then->format('Y');
  $years_now = $now->format('Y');
  $years = $years_now - $years_then;

  $months_then = $then->format('m');
  $months_now = $now->format('m');
  $months = $months_now - $months_then;

  $days_then = $then->format('d');
  $days_now = $now->format('d');
  $days = $days_now - $days_then;

  $hours_then = $then->format('H');
  $hours_now = $now->format('H');
  $hours = $hours_now - $hours_then;

  $minutes_then = $then->format('i');
  $minutes_now = $now->format('i');
  $minutes = $minutes_now - $minutes_then;

  $seconds_then = $then->format('s');
  $seconds_now = $now->format('s');
  $seconds = $seconds_now - $seconds_then;

  if ($seconds < 0) {
    $minutes -= 1;
    $seconds += 60;
  }
  if ($minutes < 0) {
    $hours -= 1;
    $minutes += 60;
  }
  if ($hours < 0) {
    $days -= 1;
    $hours += 24;
  }
  $months_last = $months_now - 1;
  if ($months_now == 1) {
    $years_now -= 1;
    $months_last = 12;
  }
  // Thank you, second grade. ;)
  if ($months_last == 9 || $months_last == 4 || $months_last == 6 || $months_last == 11) {
    $days_last_month = 30;
  }
  else if ($months_last == 2) {
    if (($years_now % 4) == 0) {
      $days_last_month = 29;
    }
    else {
      $days_last_month = 28;
    }
  }
  else {
    $days_last_month = 31;
  }
  if ($days < 0) {
    $months -= 1;
    $days += $days_last_month;
  }
  if ($months < 0) {
    $years -= 1;
    $months += 12;
  }
  return array('years' => $years, 'months' => $months, 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds);
}

Upvotes: 12

crownedzero
crownedzero

Reputation: 506

Any particular reason you're doing this the long way? There are builtin functions to get the epoch time and convert it.

#Getting current epoch time in PHP
time()  // current Unix timestamp 

#Convert from epoch to human readable date in PHP
$epoch = 1340000000;
echo date('r', $epoch); // output as RFC 2822 date - returns local time
echo gmdate('r', $epoch); // returns GMT/UTC time    

#Use the DateTime class.
$epoch = 1344988800; 
$dt = new DateTime("@$epoch");  // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00     

Upvotes: 0

Alma Do
Alma Do

Reputation: 37365

You don't need to reinvent the wheel. Use DateTime, like this:

function getInterval($seconds)
{
   $obj = new DateTime();
   $obj->setTimeStamp(time()+$seconds);
   return (array)$obj->diff(new DateTime());
}
//var_dump(getInterval(300));

-you may want check what fields will be in result and select only those that you really need

Upvotes: 5

Related Questions