-3

My mission is to display the correct day to the correct date for every month. I am so stuck, i would really appreciate some help or pointers in the right way. So far i get out all the days in October since it is october, but i also want to match the days in the loop to the correct date. Am i going this the wrong way?

All i manage to do is to display the same day on every date.

$months = date("n");
$monthsDays = array (
    1 => 31,
    2 => 28,
    3 => 31,
    4 => 30,
    5 => 31,
    6 => 30,
    7 => 31,
    8 => 31,
    9 => 30,
    10 => 31,
    11 => 30,
    12 => 31
);
$day_of_the_week = array (
    1 => "Måndag",
    2 => "Tisdag",
    3 => "Onsdag",
    4 => "Torsdag",
    5 => "Fredag",
    6 => "Lördag",
    7 => "Söndag"
);
$dayInteger = date('N', time());
echo $day_of_the_week[$dayInteger];
$day_of_the_week = date("D");
$weekNumber = date("W");
$year = date("Y");
foreach($monthsDays as $key=>$value) {
    if($key == $months) {
        echo date("M")."<br>";
        for($i = 1; $i <= $value; $i++) {
           echo '<div class="displayDate">'.$i.'</div>';
        } 
    }
}
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Pontus
  • 61
  • 1
  • 7
  • 2
    http://xyproblem.info/ possibly. Whats the reason for doing it this way? – Adam Oct 10 '18 at 13:54
  • How many days the month for a given timestamp has, you can easily determine via the `t` format specifier for the date function. (Your “static” solution of keeping those numbers in an array would be wrong about every four years in `2 => 28` anyway.) And if you want to change the language of the weekdays you output into Danish(?), check https://stackoverflow.com/questions/6910912/change-the-language-for-date-in-php (or if that’s another language, add some own research based on that.) – misorude Oct 10 '18 at 14:00
  • What are you actually trying to go? What is an input/output supposed to be? – Andreas Oct 10 '18 at 14:01
  • Im trying to build a calendar that displays the actual dates with the correct days. – Pontus Oct 10 '18 at 14:02
  • I bet there is good php librarys that can do that. Have you looked at it? – Andreas Oct 10 '18 at 14:03
  • No i havent, beacuse people told me it's real easy to do this on your own, and im trying to learn to code. So i tought i would do it on my own :P Right now i have on page, 31 white boxes that displays every single day, and the mission is to get the the correct day in to every single box :P – Pontus Oct 10 '18 at 14:07

2 Answers2

0

try this

echo  cal_days_in_month(CAL_GREGORIAN, 8, 2018)

// 8 is month number
// 2018 is year

Saurabh Sharma
  • 430
  • 2
  • 11
0

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.

JustCarty
  • 3,839
  • 5
  • 31
  • 51