PHP - strtotime和date导致长时间执行和超时


PHP - strtotime and date causes long execution and timeout

我有一个MySQL查询选择,然后循环通过所有记录(约200行)。每行有2个日期单元格,如下所示:

date('d/m/Y', strtotime($row['date_created']));
date('d/m/Y', strtotime($row['date_edited']));

问题是,这两个原因导致脚本运行很长时间,最终超时。在检查服务器日志时,我发现了这些错误:

(70007)The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed
ap_pass_brigade failed with error 70007: The timeout specified has expired

db表中的两个字段都指定为datetime。在不修改日期格式的情况下显示它们效果很好。是什么导致了这个问题?如何解决?以前从未遇到过这些日期问题。

下面是一个简化的查询:
$result = mysql_query("SELECT date_created, date_edited FROM news");
while($row = mysql_fetch_array($result)) {
    //date lines above here
}

如果没有strtotime和date,它立即打印出来,没有延迟。

真奇怪。

你可以尝试在SQL查询中使用DATE_FORMAT()格式化日期。

$result = mysql_query( "SELECT DATE_FORMAT( date_created, '%d/%m/%Y' ) as date_created, DATE_FORMAT( date_edited, '%d/%m/%Y' ) as date_edited FROM news" );
while( $row = mysql_fetch_array( $result ) ) {
    //print as-it-is
}