下面的代碼應該可以工作。我假設$ datetime1總是小於$ datetime2。
$datetime1 = "2012-07-20 12:00:00";
$datetime2 = "2012-07-23 12:00:00";
$timestamp1 = strtotime($datetime1);
$timestamp2 = strtotime($datetime2);
$weekend = array(0, 6);
if(in_array(date("w", $timestamp1), $weekend) || in_array(date("w", $timestamp2), $weekend))
{
//one of the dates is weekend, return 0?
return 0;
}
$diff = $timestamp2 - $timestamp1;
$one_day = 60 * 60 * 24; //number of seconds in the day
if($diff < $one_day)
{
return floor($diff/3600);
}
$days_between = floor($diff/$one_day);
$remove_days = 0;
for($i = 1; $i <= $days_between; $i++)
{
$next_day = $timestamp1 + ($i * $one_day);
if(in_array(date("w", $next_day), $weekend))
{
$remove_days++;
}
}
return floor(($diff - ($remove_days * $one_day))/3600);
這似乎不起作用。當我使用我的例子時,它返回0.如果我將date1更改爲19/7/12,那麼最終值將變爲960 – CelebornThingol 2012-07-26 12:33:51
哎對不起,代碼中有一個小錯誤。它應該現在工作正常。 – 2012-07-26 12:51:50