Supposedly on the web, the following line of code setlocale(LC_TIME,"es_ES");
converted everything to Spanish, but it's not like that, I don't know what other parameter should be added.
currently the following line $calendar .= '<h2 class="monthYear">'.strftime($monthName).' '.$year.'</h2>';
prints November 2020
what I want is to be able to change the language easily as well as date_default_timezone_set
determine a time zone according to the parameters or data added to it.
Given what I want to achieve in the code, how can I make that function setlocale(LC_TIME,"es_ES");
or what other change should I add or correct in order to change the language globally.
this is my code
<?php
date_default_timezone_set('America/Guayaquil');
setlocale(LC_TIME,"es_ES");
//Check date register
function checkSlots(){
$TotalBookings = 0;
return $TotalBookings;
}
//Print calendar //$month = 11; //$year = 2020;
function BuildCalendar($month, $year){
//Lunes martes Miércoles Jueves Viernes Sábado Domingo // Monday Tuesday Wednesday Thursday Friday Saturday Sunday
// Create array containing abbreviations of days of week.
$daysOfWeek = array('LUN.','MAR.','MIÉ.','JUE.','VIE.','SÁB.','DOM.');
// What is the first day of the month in question?
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
// How many days does this month contain?
$numberDays = date('t',$firstDayOfMonth);
// Retrieve some information about the first day of the
// month in question.
$dateComponents = getdate($firstDayOfMonth);
// What is the name of the month in question?
$monthName = $dateComponents['month'];
// What is the index value (0-6) of the first day of the
// month in question.
$dayOfWeek = $dateComponents['wday'];
if($dayOfWeek==0){
$dayOfWeek = 6;
} else {
$dayOfWeek = $dayOfWeek-1;
}
// Create the table tag opener and day headers
$datetoday = date('Y-m-d');
$calendar = "<table class='table-calendar'>";
$calendar .="<center>";
$calendar .= '<h2 class="monthYear">'.strftime($monthName).' '.$year.'</h2>';
//btn btn-xs btn-primary
//$calendar.= "<a class='changemonth' data-target='?month=".date('m', mktime(0, 0, 0, $month-1, 1, $year))."&year=".date('Y', mktime(0, 0, 0, $month-1, 1, $year))."'>Mes anterior</a> ";
//$calendar.= " <a class='changemonth' data-target='?month=".date('m')."&year=".date('Y')."'>Mes actual</a> ";
//$calendar.= "<a class='changemonth' data-target='?month=".date('m', mktime(0, 0, 0, $month+1, 1, $year))."&year=".date('Y', mktime(0, 0, 0, $month+1, 1, $year))."'>Mes siguiente</a><br>";
$calendar .="</center>";
$calendar .= "<tr>";
// Create the calendar headers
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header_weekday'>$day</th>";
}
// Create the rest of the calendar
// Initiate the day counter, starting with the 1st.
$currentDay = 1;
$calendar .= "</tr><tr>";
// The variable $dayOfWeek is used to
// ensure that the calendar
// display consists of exactly 7 columns.
if ($dayOfWeek > 0) {
for($k=0;$k<$dayOfWeek;$k++){
$calendar .= "<td class='empty'></td>";
}
}
$month = str_pad($month, 2, "0", STR_PAD_LEFT);
while ($currentDay <= $numberDays) {
// Seventh column (Saturday) reached. Start a new row.
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
$currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
$date = "$year-$month-$currentDayRel";
$dayName = strtolower(date('l', strtotime($date)));
//$eventNum = 0;
$today = $date==date('Y-m-d')? "today" : "";
if ($date<date('Y-m-d')) {
$calendar.="<td class='$today'><h4>$currentDay</h4> <button class='btn-danger'>N/A</button>";
} elseif($dayName=='monday' || $dayName=='tuesday' || $dayName=='wednesday' || $dayName=='thursday' || $dayName=='friday') {
$TotalBookings = checkSlots();
if($TotalBookings==4) {
$calendar.="<td class='$today'><h4>$currentDay</h4> <a href='#' class='btn-danger'>Reservado</a>";
} else {
$availableslots = 4 - $TotalBookings;
$calendar.="<td class='$today'><h4>$currentDay</h4> <a id='dateCalendar' class='dateCalendar' data-target='".$date."'>Disponible</a> <small><i>$availableslots Turnos</i></small>";
}
}elseif($dayName=='saturday' || $dayName=='sunday') {
$TotalBookings = checkSlots();
if($TotalBookings==4) {
$calendar.="<td class='$today'><h4>$currentDay</h4> <a href='#' class='btn-danger'>Reservado</a>";
} else {
$availableslots = 4 - $TotalBookings;
$calendar.="<td class='$today'><h4>$currentDay</h4> <a id='dateCalendar' class='dateCalendar' data-target='".$date."'>Disponible</a> <small><i>$availableslots Turnos</i></small>";
}
}
$calendar .="</td>";
// Increment counters
$currentDay++;
$dayOfWeek++;
}
if($daysOfWeek<7){
$remainingDays = 7 - $dayOfWeek;
for($i=0;$i<$remainingDays;$i++){
$calendar .= "<td class='empty'></td>";
}
}
$calendar .= "</tr>";
$calendar .= "</table>";
return $calendar;
}
#Print rows date //$_GET
$dateComponents = getdate();
if(isset($_GET['month']) && isset($_GET['year'])){
$month = $_GET['month'];
$year = $_GET['year'];
}else{
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
}
echo BuildCalendar($month, $year);
?>
<style type="text/css">
.today {
background-color: yellow;
}
</style>
I have tried to reduce your example to the minimum expression, since there are many things that do not interest us:
The problem here is that it
strftime
expects a string with marks and a timestamp. And you're just passing it "November" which means nothing to the show.An example that should work for you is:
Well, there is not much that needs to be changed really.
I launched your code, and edited a couple of things:
- For some reason, setlocale() doesn't work well with "es_ES", so just use "es"
- The other problem is that you are not applying
strftime()
correctly.For it to work properly, it must have the respective format change,
in this case, you want the month with respect to
setlocale()
("%B")But in addition to that, you are passing it a
string
which is the month, so that month is respected , you must parse it totime
. In the end it would be something like this:Finally, the result I'm getting with this is: