Is this possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paolone
Forum Commoner
Posts: 37
Joined: Tue Jun 11, 2002 10:18 am

Is this possible?

Post by paolone »

Hello... I'm wondering how to get the date text out of a file name that is being upload on a server, and adding this value to the database itself...

I mean :

A file called XX_XXXXX_XXXXX_XXX_03042005.csv is generated every day. And the date is the last digits. When inserting the values stored in in a database, can i add in an extra field where i can store the date value?

I think i can use the explode funtion to breack the name apart, but then?? How to store the last digit? Maybe assigning each value to an array?
:?

If this is a solution, is there a better one?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Two options...

a) If the string (filename) is always the same length you can just take the substr() of the relevant part

b) If you need something more foolproof then use a Regular Expression (shown below)...

Code: Select all

$filename = "XX_XXXXX_XXXXX_XXX_03042005.csv";
preg_match('/_(\d{8})\.csv$/', $filename, $matches); //Extract the date
$date = $matches[1]; //This is your date 
echo $date;
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

erm, dont really understand what the problem is here, its simple

explode on the filename to get the name
run an insert/update query on the database

seems simple enough to me, am i missing something?
paolone
Forum Commoner
Posts: 37
Joined: Tue Jun 11, 2002 10:18 am

Post by paolone »

Hey d11wtq, the same result but my code (of course) is longer...

Code: Select all

$filename  = "Transaction_Response_Time_Monitoring_07032005.csv";
//separate and collect to an array each element separated by _
$namepart = explode("_", $filename);
//get last one containing the date plus the extensiona and again explode it
$date_container = explode(".", $namepart[4]);
//assign the value (text only) to the variable
$date = $date_container[0];

echo "<pre>";
echo "namepart[0] =".$namepart[0]."<br>"; 
echo "namepart[1] =".$namepart[1]."<br>"; 
echo "namepart[2] =".$namepart[2]."<br>"; 
echo "namepart[3] =".$namepart[3]."<br>"; 
echo "namepart[4] =".$namepart[4]."<br>"; 
echo "The resulting date is <b>".$date."</b><br>";
But then once i got this, how can i insert this in the database while uploading? Malcolmboston, i get your advice, but can i do it at the same time of the upload or do i have to set up another step?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

yes you would need to do it in steps, at least i think so and that would definitely be my method

i would also split up the code into functions for eg

Code: Select all

function GetDate ($string)
{
// function to get date from string here
return $string;
}

function InsertDateMySQL ($string)
{
// function to insert into MySQL
}
then a simple call like:

Code: Select all

$string = GetDate ($string = "somethinghere");
InsertDateMySQL ($string);
all it requires is a simple passing of vars from one function to another.

doing it this way makes your code easy to change in the future too.
paolone
Forum Commoner
Posts: 37
Joined: Tue Jun 11, 2002 10:18 am

Post by paolone »

guys, first of all let me tell you that this forum rules... :D

thanks to your help i'm getting to the result expected !

Now i'm checking how to work on the dateformat, as it is returned as 02032005 (ddmmyyyy) and i want to insert it in a MySQL database in a different format... like yyyy-mm-dd

Unfortunatelly i've not yet installed PHP5 on the server...
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Code: Select all

$orig = "02032005";
$new = substr($orig, 4, 4) . substr($orig, 2, 2) . substr($orig, 0, 2);
^_-
Post Reply