开发者

Formatting Duration (time) in PHP

Outputting from a directions api I have a duration it will take the user to get from a to b. At the moment it is minutes but if the users journey will take 3 hours and 20 minutes it will output 200 minutes.

I would like it to work out that that is greater than 60 minutes. then divide by 60 and add the remainder to give

3 hours 20 开发者_运维技巧minutes.

How do we do this.

Marvellous


with php >= 5.3.0 you could do that :

$dt = new DateTime();
$dt->add(new DateInterval('PT200M'));
$interval = $dt->diff(new DateTime());
echo $interval->format('%Hh %Im %Ss');

Output (on my locale) : 02h 40m 00s

source : http://php.net/manual/en/class.dateinterval.php


function getNiceDuration($durationInSeconds) {

  $duration = '';
  $days = floor($durationInSeconds / 86400);
  $durationInSeconds -= $days * 86400;
  $hours = floor($durationInSeconds / 3600);
  $durationInSeconds -= $hours * 3600;
  $minutes = floor($durationInSeconds / 60);
  $seconds = $durationInSeconds - $minutes * 60;

  if($days > 0) {
    $duration .= $days . ' days';
  }
  if($hours > 0) {
    $duration .= ' ' . $hours . ' hours';
  }
  if($minutes > 0) {
    $duration .= ' ' . $minutes . ' minutes';
  }
  if($seconds > 0) {
    $duration .= ' ' . $seconds . ' seconds';
  }
  return $duration;
}


In case you have duration in seconds, you can format it with PHP gmdate() function:

echo gmdate("H:i:s", $seconds);

(Note: works for durations up to 24 hours)


$minutes = 200;
if ($minutes >= 60)
{
  $hours = (int)($minutes / 60);
  $minutes = $minutes % 60;
}


<?php

$time = 200;    // minutes
if ($time > 60) {
    $minutes = $time % 60;
$hours = ($time - $minutes) / 60;
}
echo "$hours hours $minutes minutes";

?>

Use modulo division :)


function format_minutes($value)
{
    $hours = intval($value / 60);
    $minutes = $value % 60;

    if ($hours != 0) {
        $str = $hours . ' hour';

        // Handle pluralisation.
        if (abs($hours) != 1) {
            $str .= 's';
        }
    }

    // Always show minutes if there are no hours.
    if ($minutes != 0 || $hours == 0) {
        $str .= ' ' . $minutes . ' minute';

        // Handle pluralisation.
        if (abs($minutes) != 1) {
            $str .= 's';
        }
    }

    // There will be a leading space if hours is zero.
    return trim($str);
}


Thanks for the function. I needed to change it to make it work better: The minutes were over 60:

    $seconds = $secs % 60;
    $hours   = floor($secs / 3600); 
    $secs = $secs - $hours*3600; 
    $minutes = floor($secs / 60);

Best


For those who need it... Minutes to Period of Time - PT:

<?php
function pttime($time, $format)
{
    if ($time < 1) {
        return;
    }
    $hours = floor($time / 60);
    $minutes = ($time % 60);

    //is PT
    if ($format == 'PT') {

        //full hour
        if (($hours > 0) && ($minutes == 0)) {
            $time_result = 'PT' . $hours . 'H';
        }

        //hour and minutes
        if (($hours > 0) && ($minutes <> 0)) {
            $time_result = 'PT' . $hours . 'H' . $minutes . 'M';
        }

        //just minutes
        if ($hours == 0) {
            $time_result = 'PT' . $minutes . 'M';
        }
    }

    //it's not PT
    else {
        $time_result = sprintf("%02s", $hours) . ':' . sprintf("%02s", $minutes);

    }
    return $time_result;
}

//input in minutes and its outputs
echo pttime(155,'PT'); //output -> PT2H35M
echo pttime(52,'PT');  //output -> PT52M
echo pttime(60,'PT');  //output -> PT1H
echo pttime(60,'');    //output -> 01:00


A simple duration-to-formatted-time example (with the duration given in seconds, and the output formatted as [##:]##:##):

public function format_duration($secs, $delimiter = ':')
{
    $seconds = $secs % 60;
    $minutes = floor($secs / 60);
    $hours   = floor($secs / 3600);

    $seconds = str_pad($seconds, 2, "0", STR_PAD_LEFT);
    $minutes = str_pad($minutes, 2, "0", STR_PAD_LEFT).$delimiter;
    $hours   = ($hours > 0) ? str_pad($hours, 2, "0", STR_PAD_LEFT).$delimiter : '';

    return "$hours$minutes$seconds";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜