You are here:  » date coming in wrong

date coming in wrong

Submitted by makenoiz on Mon, 2013-01-21 07:16 in

My date data is coming in wrong.
Im parsing and xml to csv. I should be getting date ranges like this

but I get this

You demo program runs it correctly. What can I change in my code? I checked to make sure it was not the data from the vendor.

Here is my code

echo 'Write XML to CSV </br>';
//////////// define files and variables
$basenameLong ='http://urltomyxmlFile.xml';
$csv = 'xmlAcmeCategory3.csv';
$i =0;
///////////////// function here
 function myRecordHandler($record)
   global $csv;
   global $i;
    // strip commas, new-line and carriage return characters from all fields
    foreach($record as $key => $value)
$record[$key] = str_replace(","," ",$value);
      $record[$key] = str_replace("\n"," ",$value);
      $record[$key] = str_replace("\r"," ",$value);
/// open csv file
$csv = fopen($csv, 'w+'); //create new CSV file if not exists else append
if (!$csv) { print "Could not create output file - check permissions!";exit(); }
  echo '</br>'.$i .' Records Processed.</br>';
  echo '</br>done!'

Thank you for your help

Submitted by support on Mon, 2013-01-21 09:01

Hello makenoiz,

There's nothing in the above script that could possibly convert "10-11" into 11-Oct, so I am assuming that you are looking at the dates you are ending up with after processing your CSV file, and I think that is where the conversion will be going wrong.

If you're importing the subsequent CSV into MySQL DATE type field for example, it's unlikely that would be robust as "10-11" is not a standard date format (note that it is ambiguous) so I would check your CSV import process and rather than have it attempt to convert to a date for you, import it as a text (VARCHAR) type if using MySQL so that there is no mis-interpretation of the data...

Let me know if you're still not sure or would like any advise on modifying the field at XML to CSV conversion time.


Submitted by makenoiz on Tue, 2013-01-22 07:34

I am simply writing to a csv file and opening the csv file in notepad ( to remove formatting ) And there it is 11-OCT.

I dont think notepad would do that?

Submitted by support on Tue, 2013-01-22 09:10

Hi makenoiz,

Neither the parser, nor Notepad would have any idea that "11-10" is intended to be a date. I know it can be hard to study XML but if you would like me to look at the actual XML source for you I will take a look (i'll remove the URL before publishing your reply...)


Submitted by makenoiz on Wed, 2013-01-23 23:35

Hi its not intended to be brought in as a date - just "10-11". Sorry I dont think I was clear. The data from the XML directly is 10-11. But Im getting Oct-11 from the parser.

URL: {link saved} You can run this from your browser as well.

but the parser is bringing it in as OCT-11. That is how its coming into notepad when I open the CSV. . I want it to come in as 10-11.

Yes please look at the actual data ( please remove actual URL ;) ) - you can run the data on your demo and you see that it appears correctly as 10-11. But when I run my script, I get Oct-11.

Thank you.

Submitted by support on Thu, 2013-01-24 09:21

Hi makenoiz,

Thanks for the link, i'll follow up by email with a copy of the .csv as generated when I ran your test script, there's definitely no alteration of 10-11 whatsoever, so I'm wondering if you may be viewing the file not in notepad but in Microsoft Excel, which I think is the default handler for .csv files on Windows, and Excel certainly _will_ attempt to convert 10-11 into a date!