My problem 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 this2018-04-22.
I divide that data into 3 with explode
, because I have to show the 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 that I have made 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 me ENERO
, and the month of the date is 04
, that is, April. I've echoed the variable $mes
I pass to the function here:
$mes_ES = FechaCastellano($mes);
and indeed it contains a 04, and yet it is printing me January, and I don't really know why.
I get the $month variable 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 I can print each data separately. But the problem is that it does not print in letters the month that I am passing it in numbers.
@DevJoel has given you the reason why the code is not working as expected.
Reading the exposed problem, I would create a
clase
utility (for my own library), and manage aDateTime
. Its flexibility would give a unique scope to this class, which could be useful for other things in the future, such as translating the names of the days, a complete representation of the date, to your liking, etc.Using the facilities offered by , the class could also incorporate
DateTime
previous validations of the string that it will receive as a parameter in the constructor. In short, an object in itself that you can use safely in any of your applications.This would be a simplified example of the class:
VER DEMO
Test code:
Result:
The problem is quite simple, the function
strtotime
expects a date in string format as a parameter, this is where your error is since it is not passing this but only the value of the month"04"
.So up to this point there is an unnecessary additional step since if you pass the date directly without doing the it will
explode
work (without modifying its function)Demo
If you want to continue doing it with
explode
, it would not be necessary to use both of them,arrays
just one will suffice since you would be directly passing the index of the month to obtain.Demo
In addition to the above, I add the way to configure the locale in a Linux Ubuntu server
to see the premises
to generate a locale (requires root)
once generated it can be used in PHP
result: