0

I've written a php code that accepts month from user and returns number of days in that month. This is the php code:

<?php

// function to check leap year
function is_leap_year($year) {
    return (( ($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0) ));
}

//array of month and day
$month_day = [
    'jan' => 30,
    'mar' => 30,
    'apr' => 31,
    'may' => 30,
    'jun' => 31
];

//store current year in a variable
$current_year = date('Y');

//if current year is leap year
if (is_leap_year($current_year)) {
    //then, 29 days for february
    $month_day['feb'] = 29;
} 
else {
    //else, 28 days for feb
    $month_day['feb'] = 28;
}
?>
<!-- html forn -->
<form action="">
    <select>
        <?php 
            //looping into array to create option tag
            foreach ($month_day as $month => $days) {
                echo "<option>$month</option>";
            }

        ?>
    </select>
    <input type="submit">
</form>

the output of that code is this: picture showing output of the above code

picture showing output of the above code

but i need output like this: picture showing the required output

picture showing the required output.

how to assign second order to feb element of the $month_day array? If its not possible, then how to use that if condition as ternary operator inside array? I've seen some threads in this site regarding 'using if inside array' but it gives me error when i used. I appreciate if you answer with code showing just array section with ternary operator used.

Ravi Sachaniya
  • 1,641
  • 18
  • 20
nivan
  • 5
  • 1
  • To get the number of days in a month use [cal_cal_days_in_month](https://www.w3schools.com/php/func_cal_cal_days_in_month.asp) – Murad Hasan Jul 03 '17 at 06:35
  • @FrayneKonok - Best to link to the [PHP Manual for cal_days_in_month](http://php.net/cal_days_in_month) instead of W3School. – Tigger Jul 03 '17 at 06:37

4 Answers4

0

Simply put it in the initial array?

$month_day = [
    'jan' => 30,
    'feb'=> is_leap_year($current_year) ? 29 : 28,
    'mar' => 30,
    'apr' => 31,
    'may' => 30,
    'jun' => 31
];
Marty
  • 39,033
  • 19
  • 93
  • 162
0

Try How-to-insert-element-into-arrays-at-specific-position but you can do it by placing feb while making array like,

$current_year = date('Y');
//array of month and day
$month_day = [
    'jan' => 30,
    'feb'=>is_leap_year($current_year) ? 29: 28
    'mar' => 30,
    'apr' => 31,
    'may' => 30,
    'jun' => 31
];
// now run loop here
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

There are many ways to sort an array. You easiest approach is to simply re-create the array adding all elements in the order you desire. Or you use the uasort() function to sort while keeping the index association intact.

However a much easier approach in this case is to simply create the array in the correct order right away:

<?php 
$month_day = [
    'jan' => 30,
    'feb' => is_leap_year(date('Y')) ? 29 : 28,
    'mar' => 30,
    'apr' => 31,
    'may' => 30,
    'jun' => 31
];
arkascha
  • 41,620
  • 7
  • 58
  • 90
0

Please try this

// function to check leap year
function is_leap_year($year) {
    return (( ($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0) ));
}

//array of month and day

//store current year in a variable
$current_year = date('Y');

//if current year is leap year
if (is_leap_year($current_year)) {
    //then, 29 days for february
    $month_day['feb'] = 29;
} 
else {
    //else, 28 days for feb
    $month_day['feb'] = 28;
}
$month_day = [
    'jan' => 30,
     'feb'=> $month_day['feb'],
    'mar' => 30,
    'apr' => 31,
    'may' => 30,
    'jun' => 31
];

?>
Shanu k k
  • 1,235
  • 2
  • 18
  • 43