Jump to content

Trying to download data matching current date but unable to


lovephp
Go to solution Solved by 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";
}

Edited by lovephp
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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' ";
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.