1

I have 10396080 seconds, and I want it to look like this: xxx hours, xxx minutes. Is there some function for that?

1

I have 10396080 seconds, and I want it to look like this: xxx hours, xxx minutes. Is there some function for that?

Not really. What you want is 10396080 seconds converted to 2887 hours 48 minutes, and I know of now existing function that does that.

There is format_interval().

 $seconds = 10396080;
 $string = format_interval($seconds, 2);
 echo $string;

But that will output "4 months 7 hours".

However, the following custom function:

 function _hoursminutes($interval, $granularity = 2, $langcode = NULL) {
   $units = array(
    '1 hour|@count hours' => 3600,
    '1 minute|@count minutes' => 60,
    '1 seconds|@count seconds' => 1,
  );  $output = '';
  foreach ($units as $key => $value) {
    $key = explode('|', $key);
    if ($interval >= $value) {
      $output .= ($output ? ' ' : '') .
        format_plural(floor($interval / $value), $key[0], $key[1], array(),
        array('langcode' => $langcode));
      $interval %= $value;
      $granularity--;
    }
    if ($granularity == 0) {
      break;
    }
  }
  return $output ? $output : t('0 second', array(), array('langcode' => $langcode));
}

 $seconds = 10396080;
 $string = _hoursminutes($seconds, 2);
 echo $string;

This will output: "2887 hours 48 minutes", which presumably what you want.

0

It depends what you mean - whether you want the hours and minute in the day, or an interval representation of the timestamp.

For the first, there's: format_date()

Formats a date, using a date type or a custom date format string.

e.g.

$formatted_date = format_date($timestamp, 'custom', 'H \h\o\u\r\s, m \m\i\n\u\t\e\s');

For the second, you might get some use out of format_interval:

Formats a time interval with the requested granularity.

But it might not do exactly what you want.

  • I think the OP is asking about converting an interval (as in format_interval), and not a date. – Free Radical May 5 '14 at 14:40
  • @FreeRadical Yep was just adding that bit in ;) – Clive May 5 '14 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.