Edit
I have deleted the previous answer for the sake of this, more elegant answer:
setlocale(LC_TIME, array('da_DA.UTF-8','da_DA@euro','da_DA','danish'));
$curYear = strftime('%Y');
?>
<h1><?= $curYear; ?></h1>
<?php
for ($month = 1; $month <= 12; $month++) {
$curMonth = strftime('%B', strtotime("01-{$month}-{$curYear}"));
$curMonth = ucfirst($curMonth);
$curMonth = utf8_encode($curMonth);
$totalDays = cal_days_in_month(CAL_GREGORIAN, $month, $curYear);
?>
<h2><?= $curMonth; ?></h2>
<?php for ($day = 1; $day <= $totalDays; $day++) { ?>
<?php
$monthName = ucfirst(strftime('%A', strtotime("{$day}-{$month}-{$curYear}")));
$monthName = ucfirst($monthName);
$monthName = utf8_encode($monthName);
?>
<div class="displayDate"><?= $day; ?> <?= $monthName; ?></div>
<?php } ?>
<?php } ?>
Explanation
There's a lot going on here so I will divulge:
setlocale
is a function that will set the locale language to whatever is specified.
The first parameter is what functions are to be affected, the second parameter is the locale to change to. In your case that was Danish.
strftime
is very similar to the date
function, with the exception that it will return the date in the language set by the locale.
After that it's really just iterating over days and months.
When setting $curMonth
, I use strtotime
so that I can manipulate it to extract that date in the specified language. Originally, I used DateTime::createFromFormat
, but that doesn't respect the locale that is set via setlocale
, which is why I used this hack.
$totalDays
will return the total number of days in the given month, these means we don't have to hardcode them. The advantages being leap years are accounted for, and if the days of the year change, you don't have to change anything! See cal_days_in_month
for how to use this function.
<?=
is the equivalent of <?php echo
which is a lot easier to write and read - IMO!
The only other interesting things I used are utf8_encode
and ucfirst
.
The former will convert the string to UTF-8 which is almost a standard these days. The latter will just set the first letter of string to a capital letter.
Note: it might be a good idea to use this solution for setting a capital letter:
$curMonth = mb_convert_case($curMonth, MB_CASE_TITLE);
Thanks to @julp for this answer.
For an explanation of what it does see the documentation for mb_convert_case
; but in essence it will simply convert the first letter to a capital regardless of the locale.