0

For example, I have 61 seconds and I want to convert it, so the end result will be "1 minute and 1 second". If I have 62 seconds though, it'll be "1 minute and 2 seconds" and for 14 seconds it will just be "14 seconds", you get it.

Is there a pre-made PHP function that can convert seconds like that so I won't have to make one myself? I'm not looking for something like gmdate() that will make it into "01:02", I want it to become a string that could be in a sentence.

Marked as Duplicate
  • 1,117
  • 4
  • 17
  • 39
  • Is it http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago? – u_mulder Jun 25 '17 at 18:17
  • Something close to it, I could try modifying it so it would just show the time & no text after it. Even though it accepts a date, not just an integer which represents seconds. – Marked as Duplicate Jun 25 '17 at 18:19

3 Answers3

3

I used DateInterval to convert seconds into years, months, days, hours, minutes and seconds.

<?php
function convertSecondsToEnglish($num_seconds)
{
  if (!is_numeric($num_seconds) || $num_seconds < 0) {
    throw new Exception('Number of seconds must be greater than zero.');
  }
  $now = new DateTime();
  $future = new DateTime();
  $future->modify(sprintf('+%d second', $num_seconds));

  $diff = $future->diff($now);

  $components = [];
  // years
  if ($diff->y != 0) {
    $years = $diff->y;
    $components[] = sprintf('%d year%s', $years, ($years != 1) ? 's' : '');
  }
  // months
  if ($diff->m != 0) {
    $months = $diff->m;
    $components[] = sprintf('%d month%s', $months, ($months != 1) ? 's' : '');
  }
  // days
  if ($diff->d != 0) {
    $days = $diff->d;
    $components[] = sprintf('%d day%s', $days, ($days != 1) ? 's' : '');
  }
  // hours
  if ($diff->h != 0) {
    $hours = $diff->h;
    $components[] = sprintf('%d hour%s', $hours, ($hours != 1) ? 's' : '');
  }
  // minutes
  if ($diff->i != 0) {
    $mins = $diff->i;
    $components[] = sprintf('%d minute%s', $mins, ($mins != 1) ? 's' : '');
  }
  // seconds
  if ($diff->s != 0) {
    $seconds = $diff->s;
    $components[] = sprintf('%d second%s', $seconds, ($seconds != 1) ? 's' : '');
  }

  return implode(', ', $components);

}

Example:

echo convertSecondsToEnglish(3600) . PHP_EOL;
echo convertSecondsToEnglish(32) . PHP_EOL;
echo convertSecondsToEnglish(60) . PHP_EOL;
echo convertSecondsToEnglish(1234567) . PHP_EOL;
echo convertSecondsToEnglish(12345678) . PHP_EOL;
echo convertSecondsToEnglish(87654321) . PHP_EOL;

Gives:

1 hour
32 seconds
1 minute
14 days, 6 hours, 56 minutes, 7 seconds
4 months, 19 days, 21 hours, 21 minutes, 18 seconds
2 years, 9 months, 9 days, 12 hours, 25 minutes, 21 seconds

Demo: https://eval.in/822061

EDIT:

To use the word 'and' between words:

  $to_return = '';
  if (count($components) > 2) {
    $last_item = array_pop($components);
    $to_return = implode(', ', $components);
    $to_return .= ' and ' . $last_item;
  } else {
    $to_return = implode(' and ', $components);
  }

  return $to_return;

Gives:

1 hour
32 seconds
1 minute
1 minute and 30 seconds
14 days, 6 hours, 56 minutes and 7 seconds
4 months, 19 days, 21 hours, 21 minutes and 18 seconds
2 years, 9 months, 9 days, 12 hours, 25 minutes and 21 seconds

Demo of this: https://eval.in/822330

Hope this helps :)

alistaircol
  • 1,433
  • 12
  • 22
2

You can use someting like this:

function gramFormat($seconds){

    $dt = new DateTime(gmdate("H:i:s", $seconds));

    //intval is needed to remove leading zeros
    $hour = ($dt->format('H') !=1 ? intval($dt->format('H')) . " hours" : intval($dt->format('H')) . " hour");
    $min = ($dt->format('i') !=1 ? intval($dt->format('i')) . " minutes" : intval($dt->format('i')) . " minute");
    $sec = ($dt->format('s') !=1 ? intval($dt->format('s')) . " seconds" : intval($dt->format('s')) . " second");

    return $hour . " and " . $min . " and " . $sec;
}

echo gramFormat('3601');

Output will be: 1 hour and 0 minutes and 1 second

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
mufax
  • 81
  • 3
0

I don't guess there is any premade function. Try this:

$time = //time in seconds;
if($time < 60){
  if($time > 1){
     return $time.' seconds';
  } else {
     return $time.' second';
  }
 } else {
   $timestring = '';
   $minutes = floor($time / 60);
   if($minutes > 1){
     $timestring .= $minutes.' Minutes';
   } else {
     $timestring .= $minutes.' Minute';
   }
   $seconds = $time%60;
   if($seconds > 1){
     $timestring .= $seconds.' Seconds';
    } else {
     $timestring .= $seconds.' Second';
    }
    return $timestring;
}
sphinx
  • 717
  • 1
  • 7
  • 19