Jump to content

Trying to download data matching current date but unable to


lovephp

Recommended Posts

OK friends im back yet again with new issue. from my table i am trying to download data matching current day date but somehow its not working, i save date as

`date_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

the outcome is 2014-04-04 22:13:19

 

here is my code, what i want is download data which is posted only today like 2014-04-04 so i did this but it downloads all blank

 

require_once('config.php');
$todaysdata = date("Y-m-d");
$query = sprintf('SELECT * FROM data WHERE date_time = "'.$todaysdata.'"');
$result = mysql_query($query) or die('Failed to connect to server: ' . mysql_error());
$today = date("d-m-Y");
header("Content-Type: text/csv");
header("Content-Disposition: attachment;filename=Data_Backup.$today.csv");
$row = mysql_fetch_assoc($result);
if ($row) {
    echocsv(array_keys($row));
}
while ($row) {
    echocsv($row);
    $row = mysql_fetch_assoc($result);
}
function echocsv($fields)
{
    $separator = '';
    foreach ($fields as $field) {
        if (preg_match('/\\r|\\n|,|"/', $field)) {
            $field = '"' . str_replace('"', '""', $field) . '"';
        }
        echo $separator . $field;
        $separator = ',';
    }
    echo "\r\n";
}

That's because you have no data.  Agreed? 

 

Think about what you wrote in your first post.  You know what the date_time (btw - bad field name) field holds in the table.  You know what the value you wish to compare to the records looks like.

 

Do they look alike?

1 - why are you using sprint???????

 

2 - you're thinking backwards.

 

You want to get the table's date field in a format that you can compare your $todaysdata field to, no?

If you applied your curiosity and work effort to a little research on MySQL you would find the list of MySQL functions that are available.  Especially the ones that pertain to dates.

 

 

$q = "select  * FROM data WHERE DATE_FORMAT(date_time, "%Y-%c-%d") = '$todaysdata' ";

my string works and is much simpler:

 

$q = "select  * FROM data WHERE DATE_FORMAT(date_time, '%Y-%m-%d') = '$todaysdata' ";

 

Note: I changed the quotes as well as the month spec - %m will give you 01,02... as it should be.  Note that by using dbl quotes outside you can avoid having to concatenate the variable at the end.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.