Get day from string in spanish PHP
Asked Answered
V

6

18

I'm trying with:

 setlocale(LC_ALL,"es_ES");
 $string = "24/11/2014";
 $date = DateTime::createFromFormat("d/m/Y", $string);
 echo $date->format("l");

And I'm getting Monday, which is correct but I need it in spanish, so, is there any way to retrieve this day in spanish?

Village answered 25/3, 2014 at 12:55 Comment(1)
I think the function you need is strftimeMignonmignonette
T
29

From the DateTime format page:

This method does not use locales. All output is in English.

If you need locales look into strftime Example:

setlocale(LC_ALL,"es_ES");
$string = "24/11/2014";
$date = DateTime::createFromFormat("d/m/Y", $string);
echo strftime("%A",$date->getTimestamp());
Til answered 25/3, 2014 at 13:2 Comment(3)
perfect, clean and elegantVillage
Nice answer. After learning this I realized I needed to do 'locale -a' in my shell to find out the correct name for the locale (e.g. 'es_ES.utf8'). Thought it would be useful to leave this here.Persse
Warning This function has been DEPRECATED as of PHP 8.1.0. Relying on this function is highly discouraged. Alternatives to this function include: date() IntlDateFormatter::format()Hembree
R
8

I use:

setlocale(LC_ALL, "es_ES", 'Spanish_Spain', 'Spanish');
echo iconv('ISO-8859-2', 'UTF-8', strftime("%A, %d de %B de %Y", strtotime($row['date'])));

or

setlocale(LC_ALL,"es_ES@euro","es_ES","esp");

both works. I have to use iconv to avoid strange symbols in accents, and i obtain this result:

domingo, 09 de octubre de 2016
Rebozo answered 24/9, 2016 at 10:17 Comment(0)
B
7

You can use strftime function:

setlocale( LC_ALL,"es_ES@euro","es_ES","esp" );
echo strftime( "%A %d de %B del %Y" );

or

function SpanishDate($FechaStamp)
{
   $ano = date('Y',$FechaStamp);
   $mes = date('n',$FechaStamp);
   $dia = date('d',$FechaStamp);
   $diasemana = date('w',$FechaStamp);
   $diassemanaN= array("Domingo","Lunes","Martes","Miércoles",
                  "Jueves","Viernes","Sábado");
   $mesesN=array(1=>"Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio",
             "Agosto","Septiembre","Octubre","Noviembre","Diciembre");
   return $diassemanaN[$diasemana].", $dia de ". $mesesN[$mes] ." de $ano";
}  
Bawbee answered 25/3, 2014 at 13:2 Comment(1)
Sadly strftime is now deprecated, and the intldateformatter functions need the intl extension be installed on php. So the easiest way is to use a handmade function.Transcendental
S
5

strftime has been DEPRECATED as of PHP 8.1.0

Another alternative is using IntlDateFormatter:

$formatter = new \IntlDateFormatter(
    'es_ES',
    \IntlDateFormatter::LONG,
    \IntlDateFormatter::LONG,
    'Europe/Madrid' //more in: https://www.php.net/manual/en/timezones.europe.php
);

echo $formatter->formatObject(new \DateTime(), "eeee", "es_ES");
  • note that "eeee" is a format from ICU
Specialism answered 21/9, 2022 at 22:40 Comment(1)
For those of you trying this, you need to install intl extension for php and enable it in your php.ini fileTranscendental
W
4

This is how I did it.

// Define key-value array
$days_dias = array(
'Monday'=>'Lunes',
'Tuesday'=>'Martes',
'Wednesday'=>'Miércoles',
'Thursday'=>'Jueves',
'Friday'=>'Viernes',
'Saturday'=>'Sábado',
'Sunday'=>'Domingo'
);

//lookup dia based on day name
$dia =  $days_dias[date('l', strtotime("1993-04-28"))];
Winola answered 16/1, 2018 at 21:36 Comment(0)
T
0

Digging a bit on how to do this, most of the times people use the function strftime.

Unfortunately, the strftime function has been DEPRECATED as of PHP 8.1.0 and 'is highly discouraged' to rely on it.

You have two options:

  1. Use the IntlDateFormatter function, but you need to install the intl extension for php and enable it in your php.ini file. This can be problematic in some shared production environments. The good side is you keep the output masks and if you use other languages you can easily exchange between them.

    The code would be like this:

    $d = new IntlDateFormatter('es_ES', null, null, null, null, null, 'dd MMMM y');
    print($d->format(new DateTime('2022-12-01'));
    

    and the output would be like this 01 Diciembre 2022

  2. Use a handmade function so you can reuse it several times. It is easier and faster to use, but you lose output masks and is tied to a single language.

    The code would be like this:

    function fechaEspanol($fecha)
    {
     $format = 'Y-m-d H:i:s'; //This is an optional input format mask for datetime database extracted info
     $d = DateTime::createFromFormat($format, $fecha); //A simple $d = new DateTime($fecha) can also be used
     $anio = $d->format('Y');
     $mes = $d->format('n');
     $dia = $d->format('d');
     $diasemana = $d->format('w');
    
     $diassemanaN = array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado");
     $mesesN = array(1 => "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
     return "{$diassemanaN[$diasemana]}, $dia de {$mesesN[$mes]} de $anio";
    }  
    

    If fechaEspanol('2022-12-01 12:00:00') the output would be like this 01 Diciembre 2022. This function can be optimized, but is put like this for a clearer view of what is being done.

I hope this helps someone.

Transcendental answered 14/2, 2023 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.