Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

translate (replace) date from italian to english

Posted on 2006-03-26
9
Medium Priority
?
428 Views
Last Modified: 2012-08-13
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...
0
Comment
Question by:firepol
  • 6
  • 3
9 Comments
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16293966
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
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16293981
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
 
LVL 2

Author Comment

by:firepol
ID: 16295298
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:BogoJoker
ID: 16295656
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
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16295661
0
 
LVL 2

Author Comment

by:firepol
ID: 16303427
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
 
LVL 17

Accepted Solution

by:
BogoJoker earned 800 total points
ID: 16303825
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
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16304784
=) Joe P
0
 
LVL 2

Author Comment

by:firepol
ID: 16304870
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question