Page 1 of 1

My spanish month script issue

Posted: Thu Jul 15, 2010 9:44 am
by cap2cap10
Greetings php technorati. Another small issue has reared its face. What I want to do is to translate the month from english to spanish when the date is echoed on the page. But what I am getting is the English and the Spanish month being printed at the same time. Here is the code:

Code: Select all

<?
$F = date("F");
 if (date("F") == January)
 {
 echo "Enero";
 }
  if (date("F") == February)
 {
 echo "Febrero";
 }
 if (date("F") == March)
 {
 echo "Marzo";
 }
 if (date("F") == April)
 {
 echo "Abril";
 }
 if (date("F") == May)
 {
 echo "Mayo";
 }
 if (date("F") == June)
 {
 echo "Junio";
 }
 if (date("F") == July)
 {
 echo "Julio";
 }
 if (date("F") == August)
 {
 echo "Agosto";
 }
 if (date("F") == September)
 {
 echo "Septiembre";
 }
 if (date("F") == October)
 {
 echo "Octubre";
 }
 if (date("F") == November)
 {
 echo "Noviembre";
 }
 if (date("F") == December)
 {
 echo "Diciembre";
 }
print $F ?> &nbsp;<?php print (Date("d, Y")); ?>
Here is the output of the code:

"JulioJuly 15, 2010"

Can someone show me the right way?

Thanks in advance

Batoe

Re: My spanish month script issue

Posted: Thu Jul 15, 2010 10:35 am
by VladSun
You don't need thousands of IF statements - you need a dictionary table:

Code: Select all

$F = date('F');

$dict = array
(
     'January' => 'Enero',
     'February' => 'Febrero'
...
     'November' => 'Noviembre',
     'December' => 'Diciembre',
);

echo $dict[$F];

Re: My spanish month script issue

Posted: Thu Jul 15, 2010 11:31 am
by cap2cap10
Ok, Ok, I get it, data dictionary type of associative array! Im still a novice. I haven't read through the whole manual yet!
Thanks for the heads up and the lesson! :drunk: