Jump to content

Read filename with date in php


PriteshP23

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/285350-read-filename-with-date-in-php/
Share on other sites

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

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.

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

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";
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.

 

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 />";
}

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.