php - 获取时差

发布于 2024-12-10 09:09:35 字数 1305 浏览 0 评论 0原文

我正在使用 php 脚本来计算两个给定时间之间的时间差,您可以在这里找到它: http ://www.gidnetwork.com/b-16.html

我刚刚发现脚本有一个错误:当开始时间在午夜之前[假设22:30]并且结束时间在午夜之后[让我们说 00:30]功能将无法工作。

你能帮我解决这个问题吗? 这是使用的脚本:

    function get_time_difference( $start, $end )
{
    $uts['start']      =    strtotime( $start );
    $uts['end']        =    strtotime( $end );
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
        if( $uts['end'] >= $uts['start'] )
        {
            $diff    =    $uts['end'] - $uts['start'];
            if( $days=intval((floor($diff/86400))) )
                $diff = $diff % 86400;
            if( $hours=intval((floor($diff/3600))) )
                $diff = $diff % 3600;
            if( $minutes=intval((floor($diff/60))) )
                $diff = $diff % 60;
            $diff    =    intval( $diff );            
            return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
        }
        else
        {
            trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );
        }
    }
    else
    {
        trigger_error( "Invalid date/time data detected", E_USER_WARNING );
    }
    return( false );
}

I am using a php script to calculate the time difference between two given times, which you can find here: http://www.gidnetwork.com/b-16.html

I just figured out that script has a bug: when the start time is before midnight [lets say 22:30] and the end time is after midnight [lets say 00:30] the function won't work.

Can you help me fix that?
This is the script used:

    function get_time_difference( $start, $end )
{
    $uts['start']      =    strtotime( $start );
    $uts['end']        =    strtotime( $end );
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
        if( $uts['end'] >= $uts['start'] )
        {
            $diff    =    $uts['end'] - $uts['start'];
            if( $days=intval((floor($diff/86400))) )
                $diff = $diff % 86400;
            if( $hours=intval((floor($diff/3600))) )
                $diff = $diff % 3600;
            if( $minutes=intval((floor($diff/60))) )
                $diff = $diff % 60;
            $diff    =    intval( $diff );            
            return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
        }
        else
        {
            trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );
        }
    }
    else
    {
        trigger_error( "Invalid date/time data detected", E_USER_WARNING );
    }
    return( false );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2024-12-17 09:09:35

为什么不使用 getdate() 函数呢?这样你就可以确定你的代码可以工作,无论你说它“不起作用”是什么意思。

function get_time_difference($start, $end)
{
    $uts['start']      =    strtotime($start);
    $uts['end']        =    strtotime($end);
    if ($uts['start'] !==-1 && $uts['end'] !==-1)
    {
        if( $uts['end'] >= $uts['start'] )
        {     
            return getdate($uts['end'] - $uts['start']);
        }
        else
        {
            trigger_error("Ending date/time is earlier than the start date/time", E_USER_WARNING);
        }
    }
    else
    {
        trigger_error("Invalid date/time data detected", E_USER_WARNING);
    }
    return(false);
}

Why not use getdate() function instead? You'll be sure then that your code works, whatever you mean by saying it "won't work".

function get_time_difference($start, $end)
{
    $uts['start']      =    strtotime($start);
    $uts['end']        =    strtotime($end);
    if ($uts['start'] !==-1 && $uts['end'] !==-1)
    {
        if( $uts['end'] >= $uts['start'] )
        {     
            return getdate($uts['end'] - $uts['start']);
        }
        else
        {
            trigger_error("Ending date/time is earlier than the start date/time", E_USER_WARNING);
        }
    }
    else
    {
        trigger_error("Invalid date/time data detected", E_USER_WARNING);
    }
    return(false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文