开发者

Best performance strategy for converting dates from MySQL format to Excel with PHP

I'm still sort of new to PHP, MySQL (and Zend Framework). I need to convert dates to strings in MM/DD/YYYY format in order to interface with another system.

I have this function that I have pinpointed as the major performance killer of this particular script. When the result set is fairly s开发者_运维百科mall (40 or 50 rows), this runs quickly, less than a second. But when there are more records, say 4000, this function takes about 4 minutes. Not good.

What is the best strategy for cases like this to increase performance?

public function convertDatesToExcelFormat() {
    $sql = "Select fil_id,
        BIRTH_DATE,
        WILL_DATE,
        LAST_CODICIL_DATE,
        TRUST_DATE,
        POA_DATE
        FROM adds";
    $result = $this->getAdapter()->fetchAll($sql);
    foreach ($result as $rowset => $row) {
        foreach ($row as &$val) {
            if (strpos($val, ':')) {
                $val = preg_replace('/[0-9]+:[0-9]+:[0-9]+/', '' , $val);
                $val = preg_replace('/-/', '/', $val);
                $val = substr($val, -6, 5) . '/' . substr($val, 0, 4);
            }
        }
        $data = array(
                'BIRTH_DATE'        => $row['BIRTH_DATE'],
                'WILL_DATE'         => $row['WILL_DATE'],
                'LAST_CODICIL_DATE' => $row['LAST_CODICIL_DATE'],
                'TRUST_DATE'        => $row['TRUST_DATE'],
                'POA_DATE'          => $row['POA_DATE'],
        );
        $where = $this->getAdapter()->quoteInto('fil_id = ?', $row['fil_id']);
        $this->update($data, $where);
    }
    return $this;
}


php has a very useful function called date_format which takes the form of

$newDateVar = date_format('d-M-Y', $inputDateVar);

The string 'd-M-Y' above can be cast in various formats. Have a look on the php site which gives a full breakdown of the formats available http://php.net/manual/en/function.date-format.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜