【问题标题】:Reformat php date compare function重新格式化php日期比较功能
【发布时间】:2010-01-12 14:18:51
【问题描述】:

在网上找到了以下 sn-p,目前正在我的 Web 应用程序中使用它,但是它返回的时间类似于“0:5:1”。我希望它像真实日期一样格式化输出,即:00:05:01。

猜猜有一个令人尴尬的快速解决方案来解决这个问题。 sn-p 来了:

function getTimeDifference($start, $end) {
    $uts['start']      =   $start;
    $uts['end']        =    $end;
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
        if( $uts['end'] >= $uts['start'] )
        {
            $diff    =    $uts['end'] - $uts['start'];
            if( $hours=intval((floor($diff/3600))) )
            $diff = $diff % 3600;
            if( $minutes=intval((floor($diff/60))) )
            $diff = $diff % 60;
            $diff    =    intval( $diff );
            return $hours . ':' . $minutes . ':' . $diff;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return FALSE;
    }
    return FALSE;

}

非常感谢!

【问题讨论】:

    标签: php datetime date time clock


    【解决方案1】:

    试试sprintf

    return sprintf("%02:%02d:%02d", $hours, $minutes, $diff);
    

    【讨论】:

      【解决方案2】:

      您可以使用str_pad() 用“0”填充值。

      return str_pad($hours, 2, "0", STR_PAD_LEFT); // turns '5' into '05'
      

      【讨论】:

        【解决方案3】:

        喜欢

        echo "Hours: " .sprintf( '%02d:%02d', $diff['hours'], $diff['minutes'] );
        

        我在链接中看到了类似的功能

        http://www.gidnetwork.com/b-16.html

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多