lovephp Posted April 4, 2014 Share Posted April 4, 2014 (edited) 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 April 4, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/ Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 When you say "isn't working", whatever do you mean? I'm guessing that you get no results from your query. Yes? Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474969 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 yes i meant the file does gets downloaded but the csv file remains blank no data on it Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474970 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474971 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 but if i remove $query = sprintf('SELECT * FROM data''); all data gets downloaded Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474972 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 (edited) there is data in the table and i want to compare is say today is 2014-04-04 i want to get only those rows with 2014-04-04 from date_time but the date_time stores records as 2014-04-04 22:13:19 Edited April 4, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474973 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 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' "; Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474974 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 lol you are right but slight issue i get error now as Parse error: parse error on that line Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474975 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 (edited) Change my double quotes around the format string to singles. Edited April 4, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474976 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 what is wrong here? $query = 'SELECT * FROM data WHERE DATE_FORMAT(date_time, '%Y-%c-%d') = "'.$todaysdata.'"'; still getting error Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474977 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474979 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 ok this works but again all blank not data getting downloaded $query = "select * FROM data WHERE DATE_FORMAT(date_time, '%Y-%m-%d') = '".$todaysdara."'"; Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474980 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 Why won't you use the line I gave you? Yours is flawed. Please? Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474981 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 used the one you gave it works but no data in the csv file Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474982 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 Can I see you new code now? Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474983 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 using this and it does download the csv but data does not appear on the file $query = "select * FROM data WHERE DATE_FORMAT(date_time, '%Y-%m-%d') = '$todaysdata' "; Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474985 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 No - all your code. Plus add a check of the number of rows you get from the query before continuing. Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474986 Share on other sites More sharing options...
Solution lovephp Posted April 4, 2014 Author Solution Share Posted April 4, 2014 (edited) hahaha got it now its working made a mistake $todaysdata = date("T-m-d"); instead of $todaysdata = date("Y-m-d"); cheers bro Edited April 4, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474987 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 Which is why I wanted to see your code. good luck - hth Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474988 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 thanks you been really helpful really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/287523-trying-to-download-data-matching-current-date-but-unable-to/#findComment-1474991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.