[英]How to find working hours in php?
我的時間是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;
它以負數顯示輸出值:(
嘗試這個
$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
您將首先轉換為秒:
$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;
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
將是時間差,以秒為單位。
使用以下內容:
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.