我几乎无处不在地发现这一点。 我有以下SQL语句:
SELECT COUNT(*) FROM `job_sheet`
LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
WHERE job_sheet.completion=".ORDER_COMPLETE."
AND deliveries.ship_date>job_sheet.delivery_date
这基本上告诉我哪些已完成的作业已经过delivery_date
(DUE DATE)。
但是,真正的交付日期实际上是7 business 天(假期并不重要。基本上只是跳过周末),而不是数据库中的内容。
我需要做的是在此部分添加7天:deliveries.ship_date>job_sheet.delivery_date + 7 business days
我的第一个想法是使用DATEADD()函数,但我很少使用它,并且在这种情况下如何实现它会非常混乱。
日期存储为:YYYY-MM-DD
答案 0 :(得分:2)
试试这个
SELECT COUNT(*) FROM `job_sheet`
LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
WHERE job_sheet.completion=".ORDER_COMPLETE."
AND deliveries.ship_date> DATE_ADD(`job_deliveries.ship_date` , INTERVAL 7 DAY)
答案 1 :(得分:1)
这应该有用,虽然我还没有测试过,因为我没有;要安装一个mysql实例来检查:
SELECT COUNT(*) FROM `job_sheet`
LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
WHERE job_sheet.completion=".ORDER_COMPLETE."
AND deliveries.ship_date>DATE_ADD(job_sheet.delivery_date,INTERVAL 7 DAY)
答案 2 :(得分:1)
好吧,在我到达这里的帮助下,添加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;
}
这将返回超过截止日期的货品数量,并带有可自定义的偏移量