Page 1 of 1

help for my practice

Posted: Thu Apr 23, 2009 11:15 am
by mis
i try to write a code that shows the total days of the all months in 2009. This is for practice but i could't fint how to write the total days of all months. it must be like this

the days of January is: 31
the days of February is:28
the days of March is:31
the days of April is:30
the days of May is:31
the days of June is:30
the days of July is:31
the days of August is:31
the days of September is:30
the days of October is:31
the days of November is:30
the days of December is:31

Re: help for my practice

Posted: Thu Apr 23, 2009 11:35 am
by Reviresco
First create an array:

Code: Select all

 
$days = array('January'=>31, 'February'=>28);
 
and so forth, including all the months.

Then loop through the array:

Code: Select all

 
foreach($days as $k=>$v) {
    echo 'The days of ' . $k . ' are ' . $v . '.<br />';
}
 

Re: help for my practice

Posted: Thu Apr 23, 2009 11:42 am
by mis
that works but what can i do if i want to take the year from the user. what i mean is that what will happen if i don't know how many days is February in 2020

Re: help for my practice

Posted: Thu Apr 23, 2009 11:50 am
by dethron

Code: Select all

<?
    $year = 2009;
    for($i=0; $i<12; $i++){
        echo date("t", mktime(0, 0, 0, $i, 1, $year));
        echo '<br />';
    }
?>