<?php $start = microtime(true); echo 'START:', $start, PHP_EOL; $ext = 'CSV'; $encode = 'UTF-8'; $bom = false; $shortopts = ""; $shortopts .= "x:"; // extension $shortopts .= "c::"; // encode $shortopts .= "b"; // bom $longopts = array( "ext:", // extension "encode::", // encode "bom", // bom ); $options = getopt($shortopts, $longopts); if (isset($options['x'])) { $ext = $options['x']; } if (isset($options['ext'])) { $ext = $options['ext']; } if (isset($options['c'])) { $encode = $options['c']; } if (isset($options['encode'])) { $encode = $options['encode']; } if (isset($options['b']) || isset($options['bom'])) { $bom = true; } echo 'ext=', $ext, PHP_EOL; echo 'encode=', $encode, PHP_EOL; echo 'bom=', $bom, PHP_EOL; if (isset($ext)) echo 'Extension=', strtoupper($ext), PHP_EOL; if (isset($encode)) echo 'Encode=', strtoupper($encode), PHP_EOL; if ($bom) echo 'BOM=true', PHP_EOL; if ('TSV' === $ext) { $delimiter = '\t'; } else if ('CSV' === $ext) { $delimiter = ','; } else if ('PSV' === $ext) { $delimiter = '|'; } else { echo 'Unsupported extension', PHP_EOL; return; } if ('UTF-16BE' === $encode) { $content = mb_convert_encoding($content, 'UTF-16BE', 'UTF-8'); if ($bom) { $content = pack('C*',0xFF,0xFE).$content; } } else if ('UTF-16LE' == $encode && $bom) { $content = mb_convert_encoding($content, 'UTF-16LE', 'UTF-8'); if ($bom) { $content = pack('C*',0xFE,0xFF).$content; } else { echo 'UTF-16LE BOM required.', PHP_EOL; } } else if ('UTF-8' === $encode) { if ($bom) { $content = pack('C*',0xEF,0xBB,0xBF).$content; } } $heading = array( '"Business_Unit_Code"', '"GL_Account_Number"', '"Fiscal_Year"', '"Accounting_Period"', '"Effective_Date"', '"Journal_ID"', '"Journal_ID_Line_Number"', '"JE_Type_Code"', '"JE_Line_Description"', '"JE_Sign"', '"Source"', '"Amount"', '"Amount_Currency"', '"Amount_Credit_Debit_Indicator"', '"Entered_By"', '"Entered_Date"' ); $fiscalYear = null; $accountingPeriod = null; $lines = array(); $lines[] = $heading; $sql = 'select * from GL_Detail'; foreach ($pdo->query($sql) as $row) { if ($row['fiscal_year'] !== $fiscalYear || $row['accounting_period'] !== $accountingPeriod) { if ($fiscalYear && $accountingPeriod) { echo 'fiscalYear=', $fiscalYear, ' ', ' accountingPeriod=', $accountingPeriod, ' ', implode("", explode("-", $fromDate)).'_'.implode("", explode("-", $enteredDate)), PHP_EOL; $content = ''; foreach ($lines as $line) { $content .= implode($delimiter, $line). "\r\n"; } $filename = 'gl_detail_'.implode('', explode('-', $fromDate)).'_'.implode('', explode('-', $enteredDate)).'.csv'; file_put_contents($filename, $content); $lines = array(); $lines[] = $heading; } $fromDate = $row['entered_date']; } $fiscalYear = $row['fiscal_year']; $accountingPeriod = $row['accounting_period']; $businessUnitCode = trim(sprintf('%-100s', $row['business_unit_code'])); $glAccountNumber = trim(sprintf('%-100s', $row['gl_account_number'])); $effectiveDate = trim(sprintf('%-100s', $row['effective_date'])); $journalID = trim(sprintf('%-100s', $row['journal_id'])); $journalIDLineNumber = trim(sprintf('%-100s', $row['journal_id_line_number'])); $jeTypeCode = trim(sprintf('%-100s', $row['je_type_code'])); $jeLineDescription = trim(sprintf('%-100s', $row['je_line_description'])); $jeSign = trim(sprintf('%-100s', $row['je_sign'])); $source = trim(sprintf('%-100s', $row['source'])); $amount = number_format($row['amount'], 0, '.', ''); $amountCurrency = trim(sprintf('%-100s', $row['amount_currency'])); $amountCreditDebitIndicator = trim(sprintf('%-100s', $row['amount_credit_debit_indicator'])); $enteredBy = trim(sprintf('%-100s', $row['entered_by'])); $enteredDate = trim(sprintf('%-100s', $row['entered_date'])); $lines[] = array( '"'.$businessUnitCode.'"', '"'.$glAccountNumber.'"', '"'.$fiscalYear.'"', '"'.$accountingPeriod.'"', '"'.$effectiveDate.'"', '"'.$journalID.'"', '"'.$journalIDLineNumber.'"', '"'.$jeTypeCode.'"', '"'.$jeLineDescription.'"', '"'.$jeSign.'"', '"'.$source.'"', '"'.$amount.'"', '"'.$amountCurrency.'"', '"'.$amountCreditDebitIndicator.'"', '"'.$enteredBy.'"', '"'.$enteredDate.'"' ); } echo 'fiscalYear=', $fiscalYear, ' ', ' accountingPeriod=', $accountingPeriod, ' ', implode("", explode("-", $fromDate)).'_'.implode("", explode("-", $enteredDate)), PHP_EOL; $content = ''; foreach ($lines as $line) { $content .= implode($delimiter, $line). "\r\n"; } $filename = 'gl_detail_'.implode('', explode('-', $fromDate)).'_'.implode('', explode('-', $enteredDate)).'.csv'; file_put_contents($filename, $content); $end = microtime(true); echo 'END:', $end, PHP_EOL; $etime = $end - $start; echo 'ETIME:', $etime, PHP_EOL; ?>