PriteshP23 Posted January 14, 2014 Share Posted January 14, 2014 Hello, I have csv filename with date. Everyday i have same csv file with respective date. I need to read filename (everyday date changes in filename) and do operation. For example, I have file "cells_20140106_165532.csv". I did like this to read it in general: $filename = "cells_".date('Ymd_hmi').".csv"; $file_contents = file_get_contents($filename); $importcsvsql = ""; It is taking current date instead of filename's date. It is generating empty file "cells_20140114_110109.csv" Thanks in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/ Share on other sites More sharing options...
paddy_fields Posted January 14, 2014 Share Posted January 14, 2014 (edited) Unless you have a filename that matches the exact date and time that you are requesting the file, then you won't find a matching file within your directory Calling date('Ymd_hmi') will always produce the current time and date in the format yyyymmdd-hhmmss Edited January 14, 2014 by paddyfields Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465166 Share on other sites More sharing options...
PriteshP23 Posted January 14, 2014 Author Share Posted January 14, 2014 Calling date('Ymd_hmi') will always produce the current time and date in the format yyyymmdd-hhmmss Yes, you are right. This is the question. How to make it general so that it take the value which is in filename? There are no more files. So, no need to find latest date or deleting other files like that. Thanks in advanced for your input. Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465172 Share on other sites More sharing options...
paddy_fields Posted January 14, 2014 Share Posted January 14, 2014 I'm not really getting your problem here, if you know the title of the required file, then just enter it into the $filename string $filename = "cells_20140106_165532.csv"; $file_contents = file_get_contents($filename); $importcsvsql = ""; But I'm guessing that I'm not understanding the point you're trying to get across Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465175 Share on other sites More sharing options...
Ch0cu3r Posted January 14, 2014 Share Posted January 14, 2014 (edited) If you already have the filename and want to get the date/time value from it you could do $filename = "cells_20140106_165532.csv"; list(, $date, $time) = explode('_', substr($filename, 0, -4)); $date = substr($date, 0, 4) . '/' . substr($date, 4, 2) . '/' . substr($date, 6, 2); $time = substr($time, 0, 2) . ':' . substr($time, 2, 2) . ':' . substr($time, 4, 2); echo "The file was create on $date at $time"; Edited January 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465176 Share on other sites More sharing options...
PriteshP23 Posted January 14, 2014 Author Share Posted January 14, 2014 If you already have the filename and want to get the date/time value from it you could do Good try. I like it. But there is error: Wrong parameter count for explode() It is not expected as solution because everyday it will change the filename. In addition, i have to read filename with date and later display the time from it. Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465177 Share on other sites More sharing options...
cyberRobot Posted January 14, 2014 Share Posted January 14, 2014 Is there another PHP script which creates the CSV file? If so, you could save the file names to a database. Then you would just need to query the database to get the most recent file. Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465180 Share on other sites More sharing options...
cyberRobot Posted January 14, 2014 Share Posted January 14, 2014 You could also consider using opendir() to get all the CSV file names: http://www.php.net/manual/en/function.opendir.php From there you can loop through the files to find the most recent version. Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465182 Share on other sites More sharing options...
Ch0cu3r Posted January 14, 2014 Share Posted January 14, 2014 (edited) Good try. I like it. But there is error: Wrong parameter count for explode() you may have tried the original code which had an error. Try my example again it does get the date and time from $filename. i have to read filename with date and later display the time from it. The following code will read any file that starts with cells_ and ends in a csv file extension grabbing the date & time from the filename foreach (glob("cells_*.csv") as $filename) { list(, $date, $time) = explode('_', substr($filename, 0, -4)); $date = substr($date, 0, 4) . '/' . substr($date, 4, 2) . '/' . substr($date, 6, 2); $time = substr($time, 0, 2) . ':' . substr($time, 2, 2) . ':' . substr($time, 4, 2); echo "The datetime for $filename is $date at $time<br />"; } Edited January 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465183 Share on other sites More sharing options...
Solution PriteshP23 Posted January 14, 2014 Author Solution Share Posted January 14, 2014 The following code will read any file that starts with cells_ and ends in a csv file extension grabbing the date & time from the filename Yes, this is working fine..! Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/#findComment-1465189 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.