這是我的情況。 我正在通過php文件發送記錄到數據庫。然後我用now()設置日期字段。它以這種格式添加日期:2013-08-01從MYSQL日期中取出破折號
然後我將數據庫導出到csv。它一切正常。但是,當我將它導出到csv時,我需要拿出破折號。所以我需要它閱讀:20130801
有沒有辦法做到這一點?這裏是我輸出爲csv代碼:
$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$filename = "APGE_ELEC_" . $to;
header('Content-Disposition: attachment; filename="'.$filename.'".csv"');
$hostname = ""; //SET SERVER/HOSTNAME
$dbusername = ""; //SET DATABASE USERNAME
$dbname = ""; //SET DATABASE NAME
$dbpassword = ""; //SET DATABASE USERNAME
$dbhandle = mysql_connect($hostname, $dbusername, $dbpassword)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($dbname,$dbhandle)
or die("Could not select Data Base");
//$query = "SELECT * FROM v88374 WHERE ((date >= '$from') AND (date <= '$to'))";
$query = "SELECT * FROM v88374 WHERE date >= DATE_FORMAT('" . $from . "', '%Y%m%d') AND date <= DATE_FORMAT('" . $to . "', '%Y%m%d')";
//$query = "SELECT * FROM v88374 WHERE date >= DATE_FORMAT($from) AND date <= DATE_FORMAT($to)";
$export = mysql_query ($query) or die ("Sql error : " . mysql_error());
$fields = mysql_num_fields ($export);
for ($i = 0; $i < $fields; $i++)
{
$header .= mysql_field_name($export , $i) ."|" . "\t";
}
while($row = mysql_fetch_row($export))
{
$line = '';
foreach($row as $value)
{
if ((!isset($value)) || ($value == ""))
{
$value = "\t";
}
else
{
$value = str_replace('"' , '""' , $value);
$value = $value . '|' . "\t";
}
$line .= $value;
}
$data .= trim($line) . "\n";
}
$data = str_replace("\r" , "" , $data);
if ($data == "")
{
$data = "\n(0) Records Found!\n";
}
print "$header\n$data";
exit();
$ to和$ from是用於在數據庫中搜索日期範圍的參數。所以我們可以獨自離開。
你在哪裏陷入特定? – PeeHaa