#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Date translating function problem


    I have a web page that displays upcoming soccer fixtures in our local leagues taken from a mysql database. There is an English and Spanish version. On the Spanish version I use the function below to translate the dates into Spanish. This works perfectly except for this time of year (end of December) when I show the games for the beginning weeks of January of the following year. The function does not show the January dates correctly. For example I now have a game in the data base on Saturday 5th January. This is displayed correctly on the English version but on the Spanish version it is displayed as "
    Sábado 07 Enero" . The first Saturday in January 2012 was the 7th so I suppose it is confusing the 2 years.
    Can anyone help with this - thanks in advance



    function traducefecha($fecha)
    {
    $fecha= strtotime($fecha); // convierte la fecha de formato mm/dd/yyyy a marca de tiempo
    $diasemana=date("w", $fecha);// optiene el número del dia de la semana. El 0 es domingo
    switch ($diasemana)
    {
    case "0":
    $diasemana="Domingo";
    break;
    case "1":
    $diasemana="Lunes";
    break;
    case "2":
    $diasemana="Martes";
    break;
    case "3":
    $diasemana="Miércoles";
    break;
    case "4":
    $diasemana="Jueves";
    break;
    case "5":
    $diasemana="Viernes";
    break;
    case "6":
    $diasemana="Sábado";
    break;
    }
    $dia=date("d",$fecha); // día del mes en número
    $mes=date("m",$fecha); // número del mes de 01 a 12
    switch($mes)
    {
    case "01":
    $mes="Enero";
    break;
    case "02":
    $mes="Febrero";
    break;
    case "03":
    $mes="Marzo";
    break;
    case "04":
    $mes="Abril";
    break;
    case "05":
    $mes="Mayo";
    break;
    case "06":
    $mes="Junio";
    break;
    case "07":
    $mes="Julio";
    break;
    case "08":
    $mes="Agosto";
    break;
    case "09":
    $mes="Septiembre";
    break;
    case "10":
    $mes="Octubre";
    break;
    case "11":
    $mes="Noviembre";
    break;
    case "12":
    $mes="Diciembre";
    break;
    }
    $ano=date("Y",$fecha); // optenemos el año en formato 4 digitos
    $fecha= $diasemana." ".$dia." ".$mes; // unimos el resultado en una unica cadena
    return $fecha; //enviamos la fecha al programa
    }
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    If you have control over the web server, you can install the Spanish language locale (how you do so depends on the server) after which you could simply
    PHP Code:
    setlocale(LC_ALL"es_ES.utf8""es_ES""esp_ESP""es");
    echo 
    date("whatever format you want"strtotime($fecha)); 
    Otherwise, are you absolutely sure $fecha is correct? What is its value?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    I used this function because I couldn't get it to display the date in Spanish without it.

    It works perfectly throughout the year except at this point at the end of the year when I am displaying dates for the following year.

    The example I have given is real. On the English version of the page the result of the same query is displayed as Saturday January 5th while on the Spanish version it shows Sabado 07 enero. In previous years at this time I have removed the function and changed the date format for a purely numerical one "2013-05-01" which returns the correct date. This is ok but I would prefer not to have to change it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    You still didn't answer requinix's question: What value does $fecha hold? And I'm wondering, to make sure I'm right. There is pretty much nothing different between the English/Spanish verion? Same database builds, and same site scripting? Just different language implementation? It seems the database may be a hair diff, esp if $fecha is not held the same, or perhaps when the even was entered, it was entered as 2012 on accident?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    The field data type is DATE. The actual value entered for this example is 2013-01-05.

    The query I run is the same for the two pages except that the Spanish version has the date translation function. They both take the data from the same data base.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Anyone any ideas on this?

IMN logo majestic logo threadwatch logo seochat tools logo