Question:
My drawback is the following. I am transforming months to Spanish for example:
04 = April, 05 = May and so on.
I bring the date data from a table, in a date type field that brings me this 2018-04-22.
I divide that data into 3 with explode
, because I have to show day, month and year, separately in a certificate.
But the value of the month I have to show it in letters and not in number, and for this I have done this function:
function FechaCastellano($date){
$mes_ = date('F', strtotime($date));
$_ES = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
$_EN = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$nombreMes = str_replace($_EN, $_ES, $mes_);
return $nombreMes;
}
The problem is that it is printing ENERO
, and the month of the date is 04
, that is, April. I have echoed the $mes
variable that I pass to the function here:
$mes_ES = FechaCastellano($mes);
and it does indeed contain an 04, and yet January is printing to me, and I don't really know why.
The variable $ month I get it from this:
$fecha_examen = explode("-",$funcion[$key]['fecha_examen']);
$dia = $fecha_examen[2];
$mes = $fecha_examen[1];
$year = $fecha_examen[0];
This is where I separate the date into 3 so that I can print each piece of data separately. But the problem is that it does not print the month that I am passing in numbers in letters.
Answer:
@DevJoel has given you the reason why the code does not work as expected.
Reading the exposed problem, I would create a utility clase
(for my own library), and handle a DateTime
object within it. Its flexibility would give a unique scope to this class, which could be used for other future things, such as translating the names of the days, a complete representation of the date, to your liking, etc.
Using the facilities offered by DateTime
, the class could also incorporate previous validations of the string that it will receive as a parameter in the constructor. In short, an object itself that you can safely use in any of your applications.
This would be a simplified example of the class:
/* PHP: Fechas en castellano, clase utilitaria - https://es.stackoverflow.com/q/158278/29967*/
class FechaEs {
private $objFecha;
private $M_es = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
private $M_en = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
private $D_es = array("Lunes", "Martes", "Miércoles", "Jueves", "Viernas", "Sábado", "Domingo");
private $D_en = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
public function __construct($mFecha)
{
$this->objFecha=DateTime::createFromFormat('Y-m-d', $mFecha);
}
public function getDDDD()
{
$nombreDia=$this->objFecha->format('l');
return str_replace($this->D_en,$this->D_es,$nombreDia);
}
public function getMM()
{
return $this->objFecha->format('m');
}
public function getMMMM(){
$nombreMes=$this->objFecha->format('F');
return str_replace($this->M_en,$this->M_es,$nombreMes);
}
public function getYYYY()
{
return $this->objFecha->format('Y');
}
public function getYY()
{
return $this->objFecha->format('y');
}
}
Test code:
/*Probando la clase*/
$testFecha=new FechaEs("2018-04-22");
echo "Día (DDDD): ".$testFecha->getDDDD().PHP_EOL;
echo "Mes (MM) : ".$testFecha->getMM().PHP_EOL;
echo "Mes (MMMM): ".$testFecha->getMMMM().PHP_EOL;
echo "Año (YYYY): ".$testFecha->getYYYY().PHP_EOL;
echo "Año (YY) : ".$testFecha->getYY().PHP_EOL;
echo PHP_EOL;
$testFecha=new FechaEs("2018-08-22");
echo "Día (DDDD): ".$testFecha->getDDDD().PHP_EOL;
echo "Mes (MM) : ".$testFecha->getMM().PHP_EOL;
echo "Mes (MMMM): ".$testFecha->getMMMM().PHP_EOL;
echo "Año (YYYY): ".$testFecha->getYYYY().PHP_EOL;
echo "Año (YY) : ".$testFecha->getYY().PHP_EOL;
Result:
Día (DDDD): Domingo
Mes (MM) : 04
Mes (MMMM): Abril
Año (YYYY): 2018
Año (YY) : 18
Día (DDDD): Miércoles
Mes (MM) : 08
Mes (MMMM): Agosto
Año (YYYY): 2018
Año (YY) : 18