好吧,在我在这里的帮助下,将日期添加 7 天很容易。由于我特别需要将 7 个工作日添加到某个日期,因此我不得不在脚本的其余部分中使用一些 PHP。我使用了一个自定义 DateHelper 类来完成这项工作。
class DateHelper {
var $holidays = array("2010-07-04", "2010-09-06", "2010-09-23", "2010-10-11", "2010-11-01", "2010-11-11", "2010-11-25", "2010-12-25");
const oneday = 86400;
const weekend = 172800;
function addBusinessDays($start_date, $business_days)
{
if (date('N', $start_date) == 6)
{ // If start date is on Saturday
$new_start_date = $start_date + self::weekend;
}
elseif (date('N', $start_date) == 7)
{ // If start date is on Sunday
$new_start_date = $start_date + self::oneday;
}
else
{
$new_start_date = $start_date;
}
$due_date = $new_start_date + $business_days * self::oneday;
$due_date += floor($business_days / 5) * self::weekend;
if (($business_days % 5) + date('N', $new_start_date) >= 6)
{
$due_date += self::weekend; // Add 2 days to compensate for the weekend
}
foreach($this->holidays as $holiday)
{
$time_stamp = strtotime($holiday);
// If the holiday falls between the start date and end date
// and is on a weekday
// Or if $new_start_date is on a holiday
if (($start_date <= $time_stamp && $time_stamp <= $due_date && date("N", $time_stamp) < 6) || date("Y-m-d", $new_start_date) == $holiday)
{
$due_date += self::oneday;
if (date('N', $due_date) >= 6)
{
// If due date on Saturday or Sunday
$due_date += self::weekend;
}
}
}
return $due_date;
}
}
我将这个类结合到这个:
//jobs that were shipped past their due date
public function totalShippedLate($offset = 7)
{
$sql = mysql_query("SELECT deliveries.ship_date, job_sheet.delivery_date, deliveries.qty_shipped FROM `job_sheet` LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID WHERE job_sheet.completion=".jobInfo::ORDER_COMPLETE."") or die(mysql_error());
$x = 0; // number of items shipped
while($data = mysql_fetch_array($sql))
{
$date = new DateHelper();
$offset_date = date("Y-m-d", $date->addBusinessDays(strtotime($data['delivery_date']), $offset));
if($data['ship_date']>$offset_date)
{
$x += $data['qty_shipped'];
}
}
return $x;
}
这将返回超过到期日发货的商品数量,并带有可自定义的偏移量