Here is my situation. I am sending records to database through php file. Then I have a date field being set with now(). It adds the date in this format: 2013-08-01
Then I am exporting the database to a csv. It all works fine. However, I need to take the dashes out when I am exporting it to the csv. So I need it to read: 20130801
Is there a way to do this? Here is my export to csv code:
$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 ) . "
";
}
$data = str_replace( "" , "" , $data );
if ( $data == "" )
{
$data = "
(0) Records Found!
";
}
print "$header
$data";
exit();
The $to and $from are parameters for searching for date range in the database. So we can leave that alone.
You need to select the formatted time
$query = "SELECT *, DATE_FORMAT(date,"%Y%m%d") FROM v88374 WHERE date >= DATE_FORMAT('" . $from . "', '%Y%m%d') AND date <= DATE_FORMAT('" . $to . "', '%Y%m%d')";
You could use str_replace()
like you did for the quotes, but modify it for dashes, replacing them with empty strings.
$value = str_replace( '-' , '' , $value );
This poses some danger, though. What if you have other data types that use dashes? They'd be corrupted. Another option is to use explode()
and checkdate()
to make sure it's a date
before you remove the dashes. Example:
$dt = explode("-", $value); // Yields array([0] => 2013, [1] => 08, [2] => 01)
// checkdate(int month, int day, int year)
if (checkdate($dt[1], $dt[2], $dt[0])) {
// It's a date.
$value = str_replace('-','',$value);
}
$newDate = str_replace('-', '', $oldDate)
Is that what you're looking for?
In addition php provides some awesome date formatting features: http://php.net/manual/en/function.date.php
$date="2013-08-01";
$converted_date=date("Ymd", strtotime($date));
SELECT DATE_FORMAT(datefield, '%Y%m%d')
if you want to do it directly in the database.
I tend to prefer a more OOP approach. This way if you ever need to change the format, you can easily modify it.
$date = new DateTime($date_from_mysql);
echo $date->format('Ymd'); // Or adjust the format how you like
Of course, you shouldn't be using the deprecated ext/mysql
extension for new projects anyway.