translate (replace) date from italian to english

I have a an array containing a date: $matchdate[1]

The date looks like this: 10 gennaio 2006.
I need it to be like this: 10 jan 2006. (english format)

Now I'm using this function to check (preg_match) if the string contains the month and if it's the case, replace the month with the english translation:

$postarr = array();

// translate Month from English to Italian
if (preg_match("/gennaio/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('gennaio', 'jan', $matchdate[1]); }
if (preg_match("/febbraio/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('febbraio', 'feb', $matchdate[1]); }
if (preg_match("/marzo/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('marzo', 'mar', $matchdate[1]); }
if (preg_match("/aprile/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('aprile', 'apr', $matchdate[1]); }
if (preg_match("/maggio/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('maggio', 'may', $matchdate[1]); }
if (preg_match("/giugno/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('giugno', 'jun', $matchdate[1]); }
if (preg_match("/luglio/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('luglio', 'jul', $matchdate[1]); }
if (preg_match("/agosto/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('agosto', 'aug', $matchdate[1]); }
if (preg_match("/settembre/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('settembre', 'sep', $matchdate[1]); }
if (preg_match("/ottobre/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('ottobre', 'oct', $matchdate[1]); }
if (preg_match("/novembre/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('novembre', 'nov', $matchdate[1]); }
if (preg_match("/dicembre/", $matchdate[1])) { $postarr["post_date"] = ereg_replace('dicembre', 'dec', $matchdate[1]); }

With this function I obtain the $postarr["post_date"] value in english and I have to convert it in order to put it into a mysql table (datetime field) as follows:

$postarr["post_date"] = date('Y-m-d', strtotime($postarr[post_date]));

My application just works fine using all this bunch of code... anyway I was wondering if there is a cleaner but not less performant) way to achieve this...
LVL 2
firepolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Do you mean your going from Italian to English?
Try this code:

$arrayItalian = array('gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre');
$arrayEnglish = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
for ($i = 0; $i < count($arrayItalian); $i++)
    str_replace($arrayItalian($i), $arrayEnglish($i), $matchdate[1]);

You could also remove all of the if statements and just use all the ereg_replace code.  If it doesn't find that month, it just does nothing. =)
0
BogoJokerCommented:
If you wanted to know which month it turned into, you could do 2 things:
1. Check the string, read the month it is after the loop.
2. Change the loop so that when I does a replace, store the month:

$newMonth = "";
for ($i = 0; $i < count($arrayItalian); $i++)
{
    if (str_replace($arrayItalian($i), $arrayEnglish($i), $matchdate[1]))
        $newMonth = $arrayEnglish($i);
}


Also this is from php.net under the str_replace function:
"This function returns a string or an array with all occurrences of search in subject  replaced with the given replace value. If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace()."
0
firepolAuthor Commented:
BogoJoker:

>You could also remove all of the if statements and just use all the ereg_replace code.  If it doesn't find that month, it just does nothing. =)

It's not the case... even if it doesn't find the month i get at the end a date like thuis: 1970-01-01. The only way is to use the if...

I also tried your script as follows and I get this error: Fatal error: Call to undefined function: array() in /var/www/snip/newmonth.php on line 12

<?php

$arrayItalian = array('gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre');
$arrayEnglish = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');

$matchdate = array();
$matchdate[1] = "14 gennaio 2006";

$newMonth = "";
for ($i = 0; $i < count($arrayItalian); $i++)
{
    if (str_replace($arrayItalian($i), $arrayEnglish($i), $matchdate[1]))
        $newMonth = $arrayEnglish($i);
}

$newdate = date('Y-m-d', strtotime($newMonth));

?>

what's wrong?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

BogoJokerCommented:
Call to undefined function: array()
That seems wildly impossible!

php.net describes the function:
array
(PHP 3, PHP 4, PHP 5)
array --  Create an array

Joe P
0
firepolAuthor Commented:
i know how to use an array but maybe the code that I've used (see above) was not correctly applied to your example. Can you check, does it work for you?

200 points ;)
0
BogoJokerCommented:
Here this works for me and I tested it out.

<?php
$arrayItalian = array('gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre');
$arrayEnglish = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');

$matchdate = array();
$matchdate[1] = "14 maggio 2006<br>";

for ($i = 0; $i < count($arrayItalian); $i++)
{
    $matchdate[2] = str_replace($arrayItalian[$i], $arrayEnglish[$i], $matchdate[1]);
    if ($matchdate[1] != $matchdate[2])
      break;
}

// print oldDate and newDate
echo "Debug, oldDate is $matchdate[1]";
echo "Debug, newDate is $matchdate[2]";
?>

Enjoy
Joe P
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BogoJokerCommented:
=) Joe P
0
firepolAuthor Commented:
Thanks dude, i learnt something and i personally hate repetitive code ;-) integrating your solution in my script is quite a pleasure instead of having all those repetitive if and ereg_replace statements... Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.