0

我最终试图获得两个日期(ContractDate 和 CloseDate)之间的天数差异。它们的日期已经以 0000-00-00 格式存储在 MySQL 数据库中,但我无法将它们正确插入到公式中。我正在为此和其他一些项目使用以下查询。

$q_groups = mysql_query("SELECT CONCAT(contacts.last_name, ', ', contacts.first_name) as Name,
                        loans.subject_property as SubjectProperty, loans.fKeyUserLoanOfficer,
                        loan_types.name as LoanType, loans.closed_on as CloseDate, loans.contract_date as ContractDate, loans.amount as Amount, loan_status_types.name as LoanStatus,
                        loans._id, loans.fKeyReferral
                        FROM `loans`
                        LEFT JOIN `loan_status_types` ON (loans.fKeyLoanDetail = loan_status_types._id)
                        LEFT JOIN `loan_status` ON (loan_status._id = loan_status_types.fKeyType)
                        LEFT JOIN `loan_types` ON (loans.fKeyLoanType = loan_types._id)
                        LEFT JOIN `contacts` ON (contacts._id = loans.fKeyContact)
                        WHERE loans.fKeyGroup = '{$gid}' AND loan_status.name LIKE '%Closed%'
                        ORDER BY loans.closed_on DESC")
                        or die(ErrorLog::handle(mysql_error()));

我想要做的是让上面查询中的两个日期(ContractDate 和 CloseDate)填充下面的日期(红色):

$start = strtotime("2012-08-16");
$end = strtotime("2012-06-28");
$dayz = ($start - $end) / (60 * 60 * 24);

当我查看数据库时,“close_on”字段的日期为 2012-08-16,当我查看“contract_date”字段时,日期为 2012-06-28 ...但我无法获取此信息正确插入,没有错误或根本没有数据显示。

** 我遇到的问题是......如何用字段数据替换静态日期?**

谢谢,埃德

4

4 回答 4

1
SELECT
TO_DAYS(closed_on) - TO_DAYS(contract_date) AS `dayz`

添加到您现有的查询中:

$q_groups = mysql_query("SELECT CONCAT(contacts.last_name, ', ', contacts.first_name) as Name,
                        loans.subject_property as SubjectProperty, loans.fKeyUserLoanOfficer,
                        loan_types.name as LoanType, loans.closed_on as CloseDate, loans.contract_date as ContractDate, loans.amount as Amount, loan_status_types.name as LoanStatus,
                        loans._id, loans.fKeyReferral,
                        TO_DAYS(loans.closed_on) - TO_DAYS(loans.contract_date) AS `dayz`
                        FROM `loans`
                        LEFT JOIN `loan_status_types` ON (loans.fKeyLoanDetail = loan_status_types._id)
                        LEFT JOIN `loan_status` ON (loan_status._id = loan_status_types.fKeyType)
                        LEFT JOIN `loan_types` ON (loans.fKeyLoanType = loan_types._id)
                        LEFT JOIN `contacts` ON (contacts._id = loans.fKeyContact)
                        WHERE loans.fKeyGroup = '{$gid}' AND loan_status.name LIKE '%Closed%'
                        ORDER BY loans.closed_on DESC")
                        or die(ErrorLog::handle(mysql_error()));

凭借正确性的额外好处,简单地将时间戳除以一天中的秒数将无法跨越 DST 边界等。

于 2012-08-17T00:30:57.527 回答
0

我建议你让 MySQL 做数据操作。它具有比 PHP 更好的功能范围,以及本机日期/时间数据类型。您可以用明确的语言添加和减去时间和日期,并且不会遇到往返 Unix 时间戳的问题。

而且你不想用 Unix 时间戳来做这件事!这是因为对于日期操作,它提供了太多的精度:秒 vs 天,这是一个很好的迹象,它是错误的解决方案。他们也不允许诸如时区和夏令时转换之类的事情,并且试图自己处理是一个漫长的滑坡的开始。使用正确的日期处理函数要好得多,因为它们已经被广泛调试并且可以正常工作。

您也可以使用新的 PHP DateTime库,但如果您的日期来自 MySQL 并返回 MySQL,只需使用 MySQL;它的转换较少。

于 2012-08-16T23:50:06.713 回答
0

如果你被 PHP 困住,你可以使用DateTime对象:

  $datetime1 = new DateTime('2009-10-11');  
  $datetime2 = new DateTime('2009-10-13');
  $interval = $datetime1->diff($datetime2);
  echo $interval->format('%R%a days');

DateTime::diff()手册。

于 2012-08-17T00:03:16.213 回答
0

如果您的数据库中已经有数据,我建议您使用它,因为它更快,正如 staticsan 指出的那样。

一个例子是这样的,但是您将用表中的字段数据替换下面的静态日期:

SELECT DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36') AS days;
于 2012-08-17T00:10:20.000 回答