Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从2个datetime数据库字段中回显时间差,以秒、分和小时为单位,而不仅仅是其中的一个?_Php_Mysql_Datetime - Fatal编程技术网

Php 如何从2个datetime数据库字段中回显时间差,以秒、分和小时为单位,而不仅仅是其中的一个?

Php 如何从2个datetime数据库字段中回显时间差,以秒、分和小时为单位,而不仅仅是其中的一个?,php,mysql,datetime,Php,Mysql,Datetime,我有这个,但它只能以秒、分或小时显示时差 $diff = strtotime('2015-02-10 17:03:44') - strtotime(''.$row['start_time'].''); $mins = $diff / 60; $hrs = $mins / 60; echo $diff." seconds<br />"; echo $mins." minutes<br />"; echo $hrs." hours<br />"; 与以秒为单位显

我有这个,但它只能以秒、分或小时显示时差

$diff = strtotime('2015-02-10 17:03:44') - strtotime(''.$row['start_time'].'');
$mins = $diff / 60;
$hrs = $mins / 60;
echo $diff." seconds<br />";
echo $mins." minutes<br />";
echo $hrs." hours<br />";
与以秒为单位显示总差异、以分钟为单位显示总差异、以小时为单位显示总差异不同,我想以小时、分钟和秒为单位显示总差异,如下所示:

0 hours 0 minutes 30 seconds
如果小时数小于1,则应四舍五入为0,分钟数也应如此。如果差值为分和秒,则示例如下:

0 hours 5 minutes 45 seconds
如果差异全部为3,则这将是一个示例结果:

1 hour 17 minutes 10 seconds
我如何才能做到这一点?

以下是解决方案:

$diff = "";
    if( @$params["from_date"] && @$params["to_date"] )
    {
        $days = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params ['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->d );
        $months = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->m );
        $years = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->y );
        // for years, months and days
        if($months == 0 && $years == 0 && $days >= 1) 
            $diff = $days. ' day(s)';
        elseif($months == 0 && $years >= 1 && $days == 0)  
            $diff = $years.' year(s)';
        elseif($months == 0 && $years >= 1 && $days >= 1)
            $diff = $years.' year(s) '. $days.' day(s)';
        elseif($months >= 1 && $years == 0 && $days == 0)
            $diff = $months.' month(s)';
        elseif($months >= 1 && $years == 0 && $days >= 1)
            $diff = $months.' month(s) '.$days.' day(s)';
        elseif($months >= 1 && $years >= 1 && $days == 0)
            $diff = $years. ' year(s) '.$months.' month(s) ';
        elseif($months >= 1 && $years >= 1 && $days >= 1)
            $diff = $years.' year(s) '.$months.' month(s) '.$days.' day(s)';
        else
        {   
            $diff = $years.' year(s) '.$months.' month(s) ';
        }
    }
    echo $diff;

你试过了吗?可能是重复的,不是重复的。更像是这个的复制品,它对我有用:无法让它工作。我们无法使用位于此处的不同解决方案:
$diff = "";
    if( @$params["from_date"] && @$params["to_date"] )
    {
        $days = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params ['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->d );
        $months = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->m );
        $years = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->y );
        // for years, months and days
        if($months == 0 && $years == 0 && $days >= 1) 
            $diff = $days. ' day(s)';
        elseif($months == 0 && $years >= 1 && $days == 0)  
            $diff = $years.' year(s)';
        elseif($months == 0 && $years >= 1 && $days >= 1)
            $diff = $years.' year(s) '. $days.' day(s)';
        elseif($months >= 1 && $years == 0 && $days == 0)
            $diff = $months.' month(s)';
        elseif($months >= 1 && $years == 0 && $days >= 1)
            $diff = $months.' month(s) '.$days.' day(s)';
        elseif($months >= 1 && $years >= 1 && $days == 0)
            $diff = $years. ' year(s) '.$months.' month(s) ';
        elseif($months >= 1 && $years >= 1 && $days >= 1)
            $diff = $years.' year(s) '.$months.' month(s) '.$days.' day(s)';
        else
        {   
            $diff = $years.' year(s) '.$months.' month(s) ';
        }
    }
    echo $diff;