从方向api输出我有一个持续时间,用户从a到b需要几分钟。目前是几分钟,但如果用户的旅程需要3小时20分钟,它将输出200分钟。
我希望它能超过60分钟。然后除以60并将余数相加,得到
3小时20分钟。
我们如何做到这一点。
惊人的
使用php>=5.3.0,您可以做到这一点:
$dt = new DateTime();
$dt->add(new DateInterval('PT200M'));
$interval = $dt->diff(new DateTime());
echo $interval->format('%Hh %Im %Ss');
输出(在我的区域设置上):02h 40m 00s
来源: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;
}
如果持续时间以秒为单位,可以使用PHP gmdate()
函数进行格式化:
echo gmdate("H:i:s", $seconds);
(注意:工作时间最长可达24小时)
$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";
?>
使用模除法:)
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);
}
感谢您的函数。我需要改变它,让它更好地工作:时间超过60分钟:
$seconds = $secs % 60;
$hours = floor($secs / 3600);
$secs = $secs - $hours*3600;
$minutes = floor($secs / 60);
最佳
对于那些需要它的人…时间段的分钟-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
格式化时间的简单持续时间示例(持续时间以秒为单位,输出格式为[##:]##:##):
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";
}