I am creating an hourly calendar (a booking form), and I have my calendar created
$today = time();
if (date('D', $today) === 'Mon') {
$timestamp = strtotime('this Monday');
} else{
$timestamp = strtotime('last Monday');
}
$days = array();
$dates = array();
for ($i = 0; $i < 7; $i++) {
$days[] = strftime('%A <br /> %d %b %Y', $timestamp);
$dates[] = strftime('%d %b %Y', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$return .= '<table class="reservation_time_table">
<tr><th>'.esc_html__('Hours', 'time_reservation').'</th>';
foreach ($days as $day => $day_value) {
$today_class = ( strtotime(gmdate('d M Y', $today)) == strtotime(strip_tags($day_value)) ) ? 'class="today"' : '';
$return .= '<th '.$today_class.'>'.$day_value.'</th>';
}
$return .= '</tr>';
for ($hour=8; $hour < 23 ; $hour++) {
$return .= '<tr>';
$return .= '<th>'.$hour.':00</th>';
foreach ($dates as $date => $date_value) {
$full_hour = $hour. ':00';
$table_date = strtotime($date_value. ' ' .$full_hour);
$reserved = (in_array($table_date, $full_reserved_array)) ? 'disabled' : '';
$disabled_class = ($today > $table_date) ? 'disabled' : '';
$return .= '<td class="reservation_time '. $disabled_class. ' '. $reserved .'" data-time="'.$table_date.'"></td>';
}
$return .= '</tr>';
}
$return .= '</table>';
I have an array of booked dates in my $reserved
variable.
Now since the site I'm working on is build on wordpress and translated with WPML, I can check the language of the site like:
if( in_array('sitepress-multilingual-cms/sitepress.php', get_option('active_plugins')) ){
$current_lang = ICL_LANGUAGE_CODE;
} else{
$current_lang = '';
}
So for instance for Swedish this will be sv
, for German de
etc.
So I googled a bit, and what I found is that I can change the format of my time and dates by using setlocale()
. So I tried:
if ($current_lang == 'sv') {
setlocale(LC_ALL, 'sv-SE');
} elseif($current_lang == 'de') {
setlocale(LC_ALL, 'de-DE');
} else{
setlocale(LC_ALL, 'en-US');
}
I also tried with just:
setlocale(LC_TIME, 'sv-SE');
But nothing changed on my frontend.
I tried putting my if-else
loop with $timestamp
inside (and even changed the name to match the translation), I put the for
loop in, but nothing changed.
How do I handle the translation when building calendar with php?
Ok, so the answer is that I was missing the proper locale. But that just points out the bigger issue - if the server I deploy this won't have it it won't be ok.
So the solution is to install the proper localization on server
sudo locale-gen sv_SE
sudo locale-gen sv_SE.UTF-8
or whatever language you need...