Configure locales in PHP

3

I have the following code:

<?php
   $date = new DateTime($uh->updated_at);
   echo $date->format('d/M/Y');
?>

I need to generate the date, based on the value of a variable. And that this is generated in Spanish. I tried the following, but I could not pass as an argument the date I want.

<?php
   setlocale(LC_ALL,"es_ES");
   echo strftime("%A %d de %B del %Y");
?>

How can I pass a variable with a dynamic date and that it be printed in Spanish?

    
asked by Cesar Augusto 29.07.2017 в 00:17
source

1 answer

5

We start with the code

You could try something like this:

<?php
setlocale(LC_ALL,"es_ES");

/* HOY */
$fecha = new DateTime();
print strftime("%A %d de %B del %Y", $fecha->getTimestamp()) . "\n";

/* En una semana */
$fecha = new DateTime("2017-08-04");
print strftime("%A %d de %B del %Y", $fecha->getTimestamp()) . "\n";

?>

Result

  

Friday, July 28, 2017

     

Friday, August 04, 2017

What have we done?

Analyzing the functions we are using for the date generation , in this case strftime () , we can find that Parameters of this function are:

  • format
  • timestamp

Although the format can be found in the PHP documentation The parameter timestamp can be obtained in the class DateTime () , which returns the amount of seconds elapsed since January 1, 1970 . This parameter is used by the strftime () feature to generate a message according to the specified format.

    
answered by 29.07.2017 / 01:07
source