Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 27th, 2008, 05:36 PM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 839
Default Look for partial string in keys in associative array

I am transfering data from a CSV file to a MySql database.
Unfortunately the dates are in 2008.27.08 US string format instead instead of 2008-08-27 database format.
The CSV data is first read into an associative array where the column names become the key names.
So I need to search the keys to find 'date' as part of the key name then convert the element to 'Y-m-d' format.
Is there a way of doing this other than a rather clumsy
[PHP]foreach($record as $key=>&$element)
{
if(strpos($key,'date'))
$element = date('Y-m-d',strtotime($element));
}[/PHP]
Reply
  #2  
Old August 28th, 2008, 10:30 AM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 839
Default Convert US date to MySql date

I have dates in the format 2008.08.12. They are from an oscommerce feed.
How do I convert them to a MySql format.
The following produces 2008-12-08T00:00:00+00:00 [PHP]$date = date('c',strtotime($date)); [/PHP] The month should be August not December.
Reply
  #3  
Old August 28th, 2008, 11:21 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 646
Default

It looks that replacing the "." by "-" should do the trick. I guess the dots make the date() function believe it's an american style date (with day next to year).

Maybe the comments section of the php date() function reference offer some insight (like this one php date comment)
Reply
  #4  
Old August 28th, 2008, 11:42 AM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 839
Default

Thanks Dormilich.
Replacing the dots with dashes will produce 2008-12-08 which mysql will treat as 8th of December.
The date is 12th of August.
I don't think the format 2008.12.08 as 8th August is recognised anywhere in the world!
Maybe if the server had US settings it could be recognised correctly but my server has UK settings.
Think I will try to persuade the data supplier to mend his ways
Reply
  #5  
Old August 28th, 2008, 12:09 PM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 646
Default

Quote:
Originally Posted by code green
Think I will try to persuade the data supplier to mend his ways
if that fails you can invert the date manually (like explode - invert - implode or preg_replace/preg_match). I remember seeing one comment (php.net) using something like that.
Reply
  #6  
Old August 28th, 2008, 09:08 PM
dlite922's Avatar
Site Addict
 
Join Date: Dec 2007
Location: US
Age: 23
Posts: 658
Default

FYI:

here's a basic function:

(I didn't write it, copy pasted from an app. )

[PHP]

function convertUSADateToMysql($date)
{
$mysqlDate = "";

if ($date != "")
{
$date = str_replace("/", "-", $date);

list($month, $day, $year) = explode ("-", $date);

if (strlen($day) < 2)
{
$day = "0" . $day;
}

if (strlen($month) < 2)
{
$month = "0" . $month;
}

if (strlen($year) < 3)
{
$year = "20" . $year;
}

$mysqlDate = $year . '-' . $month . '-' . $day;
}
return $mysqlDate;
}

[/PHP]



you're welcome,




Dan
Reply
  #7  
Old August 28th, 2008, 09:30 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,777
Default

Couldn't you just use a regex on the CVS file before it is read into your array?
Reply
  #8  
Old August 29th, 2008, 07:21 AM
Newbie
 
Join Date: Feb 2008
Location: India
Posts: 14
Default

If you have to put data from CSV to MySql tables then you can also do in following way.

it is my test csv file.(test.csv)
Expand|Select|Wrap|Line Numbers
  1. "0375","GF","Brentwood","2008.27.08"
  2. "0376","GF","O St. Market","2008.27.08"
  3. "0378","Gs","Tivoli Square","2008.27.08"
  4. "0383","ods","Van Ness","2008.27.08"
  5. "0379","Foods","Wisconsin Ave - GROCERY","2008.27.08"
  6.  
And this is the mysql query
Expand|Select|Wrap|Line Numbers
  1. LOAD DATA INFILE "C:/Documents and Settings/Dheeraj/Desktop/test.csv" 
  2. INTO TABLE from_csv 
  3. FIELDS TERMINATED BY "," 
  4. OPTIONALLY ENCLOSED BY '"' 
  5. LINES TERMINATED BY '\r\n'
  6. (id, name,city,@varDate)
  7. SET curDate=CONCAT(SUBSTRING_INDEX(@varDate,'.',1),'-',SUBSTRING_INDEX(SUBSTRING_INDEX(@varDate,'.',-1),'.',1),'-',SUBSTRING_INDEX(SUBSTRING_INDEX(@varDate,'.',-2),'.',1));
  8.  
And this is the structure of my table
Expand|Select|Wrap|Line Numbers
  1. id            varchar(50) 
  2. name      varchar(50)
  3. city         varchar(50)  
  4. curDate   date 
  5.  

Hope this will help you.

Best of luck.
Reply
  #9  
Old August 29th, 2008, 08:50 AM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 839
Default

Wow! Thanks for all the suggestions guys.
I will look at the merits of each one.
The CSV file is opened and read by a class so that needs to remain generic.
And the data is input via a MySql class so I don't really want to add extra load in there.
I need something to manipulate the data in between, but I am hoping to develop this into a generic class eventually so ideally,
I suppose I need a function to handle dates and convert them all to ISO 8601 format (ANSI SQL).from all standards.
Hopefully your ideas will help me write this.

This forum is excellent for bouncing ideas of people helping one to see the true problem more clearly
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles