如何在PHP中找到工作时间?

时间:2014-03-18 11:04:49

标签: php mysql

我的时间是2014-03-18 15:03:49。出门时间是2014-03-19 10:04:19。 这里怎么计算差异?

我试过如下。

$inTime = date('2014-03-18 15:03:49');
$outTime = date('2014-03-19 10:04:19');
$tot = $inTime - $outTime;

它以负数显示输出值:(

4 个答案:

答案 0 :(得分:2)

试试这个

$inTime = new DateTime('2009-10-11');
    $outTime = new DateTime('2009-10-13');
    $interval = $inTime ->diff($outTime );
    echo $interval->format('%R%a days');//difference in days

答案 1 :(得分:0)

您将首先转换为秒:

$intime = strtotime('2014-03-18 15:03:49');
$outtime = strtotime('2014-03-19 10:04:19');
$seconds = $outtime - $intime;

$hh = str_pad(floor($seconds/3600),2,0,STR_PAD_LEFT);
$mm = str_pad(floor($seconds%3600/60),2,0,STR_PAD_LEFT);
$ss = str_pad($seconds%60,2,0,STR_PAD_LEFT);

echo $hh . ':' . $mm . ':' . $ss;

答案 2 :(得分:0)

php date函数用于将unixtime格式化为字符串,因此它将返回一个字符串,而不是一个数字

如果您使用php strtotime,您将获得数字值,您可以添加和减去

$inTime = strtotime('2014-03-18 15:03:49');
$outTime = strtotime('2014-03-19 10:04:19');
$tot = $outTime - $inTime;

$tot将是时间上的差异,以秒为单位。

答案 3 :(得分:0)

使用以下内容:

$inTime = strtotime('2014-03-18 15:03:49');
$outTime = strtotime('2014-03-19 10:4:19');
$diff = $outTime - $inTime;


//convert to min and sec
$convert_min = $diff/60;
$convert_sec = $diff%  60;//seconds

//convert to hours and min
$convert_hr = floor($convert_min/60);//hours
$remainder = floor($convert_min % 60);//minutes
$total_visit =  $convert_hr.":".$remainder.":".$convert_sec; 
print_r($total_visit);
// Output will be 19:0:30 hours