Logo Luan Morina
Display current date in another language

Zondag

22

December 2024

Translate array keys (names) in PHP to another language

In PHP, you can create an associative array where the keys represent the original strings (original language, for example) and the corresponding values are the translations in the desired language. Here’s how you can display the current date in another language by translating array keys:

PHP

<div class="calendar-container"> 
<div class="calendar-header">
<span></span> 
<span></span> 
</div>
<div class="calendar-body">
<?php
$datum_vandag = getdate();
$naam_van_de_dag = array();
$naam_van_de_dag[] = "Zondag";
$naam_van_de_dag[] = "Maandag";
$naam_van_de_dag[] = "Dinsdag";
$naam_van_de_dag[] = "Woensdag";
$naam_van_de_dag[] = "Donderdag";
$naam_van_de_dag[] = "Vrijdag";
$naam_van_de_dag[] = "Zaterdag";

$naam_van_de_maand = array();
$naam_van_de_maand[] = "Januari";
$naam_van_de_maand[] = "Februari";
$naam_van_de_maand[] = "Maart";
$naam_van_de_maand[] = "April";
$naam_van_de_maand[] = "Mei";
$naam_van_de_maand[] = "Juni";
$naam_van_de_maand[] = "Juli";
$naam_van_de_maand[] = "Augustus";
$naam_van_de_maand[] = "September";
$naam_van_de_maand[] = "Oktober";
$naam_van_de_maand[] = "November";
$naam_van_de_maand[] = "December";

$dag = $naam_van_de_dag[$datum_vandag[wday]];
$datum = date('d');
$maand = $naam_van_de_maand[$datum_vandag[mon]-1];
$jaar = date('Y');
?> 

<h1><?php echo $dag;?></h1> 
<h2><?php echo $datum;?></h2> 
<h3><?php echo $maand;?> <?php echo $jaar;?></h3>

</div> 
</div>

CSS (Demo)

@import url('https://fonts.googleapis.com/css2?family=Oswald:wght@500&display=swap');

.calendar-container {
margin: 0 auto;
max-width: 260px;
font-family: "Oswald", sans-serif;
} 

.calendar-container {
margin: 0 auto;
max-width: 260px;
font-family: "Oswald", sans-serif;
} 

.calendar-header {
display: flex;
flex-direction: row;
justify-content: space-around;
background-color: rgba(204, 84, 70, 0.44); 
border-top-right-radius: 10px;
border-top-left-radius: 10px;
border-bottom: 4px solid #ffffff;
}

.calendar-header span {
background-color: rgba(237, 240, 241, 0.44);
border: 4px solid rgba(255, 255, 255, 0.44); 
width: 20px;
height: 40px;
border-radius: 6px;
position: relative;
top: -20px;
}

.calendar-body {
background-color: rgba(255, 255, 255, 0.44);
min-height: 100px;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
padding: 20px;
text-align: center;
}

.calendar-body h1 {
font-size: .80em;
margin: 0 0 4px 0;
text-transform: uppercase;
color: rgba(0, 0, 0, .5);
}

.calendar-body h2 {
font-size: 4em;
margin: 0 0 4px 0;
color: rgba(0, 0, 0, .9);
}

.calendar-body h3 {
font-size: 1.2em;
font-weight: normal;
text-transform: uppercase;
color: rgba(0, 0, 0, .5);
}
   Display the current date in another language with PHP