在PHP中格式化持续时间(时间)

在PHP中格式化持续时间(时间),php,datetime,Php,Datetime,从一个方向api的输出我有一个持续时间,它将需要用户从a到b。目前是分钟,但如果用户的旅程需要3小时20分钟,则输出200分钟 我希望它能计算出大于60分钟的时间。然后除以60,将余数相加,得到 3小时20分钟 我们如何做到这一点 妙极了 $minutes = 200; if ($minutes >= 60) { $hours = (int)($minutes / 60); $minutes = $minutes % 60; } 使用模除:)使用php>=5.3.0,您可以这样

从一个方向api的输出我有一个持续时间,它将需要用户从a到b。目前是分钟,但如果用户的旅程需要3小时20分钟,则输出200分钟

我希望它能计算出大于60分钟的时间。然后除以60,将余数相加,得到

3小时20分钟

我们如何做到这一点

妙极了


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

使用模除:)

使用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


来源:

一个简单的从持续时间到格式化时间的示例(持续时间以秒为单位,输出格式为[###:]##):


谢谢你的功能。我需要更改它以使其更好地工作: 会议记录超过60分钟:

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

最佳

如果持续时间以秒为单位,则可以使用PHP
gmdate()
函数对其进行格式化:

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

(注意:最长可工作24小时)

您可以使用
DateInterval->format

$rawInterval = 'PT200M';
$formattedInterval = (new DateInterval($rawInterval))->format('%Hh %Im %Ss');

输出为:
02h 40m 00s

供需要它的人使用。。。分钟到时间段-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

小时是
秒/3600
天是
秒/86400
非常好$秒的格式应为
四舍五入
,以达到所需的精度,否则您将得到一个非常难以处理的十进制数。如果时间小于60分钟(如45分钟),则会得到一个类似“小时45分钟”的语句,如果时间大于或等于1440分钟,则该语句不起作用,但是我想这不是问题。@Kai Sellgren OP从来没有说过他们想要以天为单位格式化,所以如果输入10059秒,它会返回“02:167:39”作为持续时间。虽然对大多数开发人员来说差异可能微不足道,使用一个单独的变量(克隆$dt)而不是实例化一个新的DateTime对象,在特定情况下可能与此相关,因为返回的时间间隔需要微秒精度。建议返回:00h 200m 00s
    $seconds = $secs % 60;
    $hours   = floor($secs / 3600); 
    $secs = $secs - $hours*3600; 
    $minutes = floor($secs / 60);
echo gmdate("H:i:s", $seconds);
$rawInterval = 'PT200M';
$formattedInterval = (new DateInterval($rawInterval))->format('%Hh %Im %Ss');
<?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