You are here:  » Help with str_replace


Help with str_replace

Submitted by ROYW1000 on Fri, 2011-08-19 12:40 in

Hi

I have the following code and want to do a find and replace on the data.

The file currently sees this Sep 1 2011 12:00AM

But I want to remove the 12:00AM and change the date to be 01/09/2011.

I am stuck on where I should put the replacement code within it.

{code saved}

Thanks
Roy

Submitted by support on Fri, 2011-08-19 12:52

Hi Roy,

Other than where filename to parse is dynamically constructed from the date I don't actually see in your code where you are attempting to use the date field from the parsed data, so let's say it's in a field called DATE so within myRecordHandler the date is contained in $record["DATE"].

PHP's strtotime() function should be able to convert the incoming format into a time value, which you can then re-format as required using the date() function. For example:

  function myRecordHandler($record)
  {
    $time = strtotime($record["DATE"]);
    $date = date("d/m/Y",$time);
  }

...and then go on to use $date in the rest of your record handler code...

Cheers,
David.

Submitted by ROYW1000 on Fri, 2011-08-19 13:27

Hi

Originally I was going to use this to reformat the date in the feed from Sep 1 2011 12:00AM to become 01/09/11 etc.

I think your method will be far easier.

As far as this is now concerned how should this code be.

extrafield73 = '".mysql_real_escape_string( $record["EXPECTED_DATE"] )."'

as this is now going to be called $time

$sql = "
UPDATE jss_products SET
extrafield73 = '".mysql_real_escape_string( $record["EXPECTED_DATE"] )."'
WHERE
code = '".mysql_real_escape_string( $record["PRODUCT_CODE"] )."' ";
}
mysql_query($sql) or die(mysql_error());

Many thanks again for the great speedy support.

Roy

Submitted by support on Fri, 2011-08-19 13:36

Hi Roy,

A modification of your snippet performing the date format change as described above would be:

$expected_time = strtotime($record["EXPECTED_DATE"]);
$expected_date = date("d/m/Y",$expected_time);
$sql = "
UPDATE jss_products SET
extrafield73 = '".mysql_real_escape_string( $expected_date )."'
WHERE
code = '".mysql_real_escape_string( $record["PRODUCT_CODE"] )."' ";
}
mysql_query($sql) or die(mysql_error());

Cheers,
David.