My spanish month script issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

My spanish month script issue

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: My spanish month script issue

Post 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];
Last edited by VladSun on Thu Jul 15, 2010 11:35 am, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: My spanish month script issue

Post 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:
Post Reply