Dynamically generate months in PHP

Sometimes we need to select month value from drop-down and for that we hardcode the month values in HTML option fields. It is possible to dynamically generate the month values using PHP and we can use those generated month values in HTML select option tag.

You may also read Dynamically generate years in PHP

Here is the example how we can generate month values in PHP technology.

Here is the output when we run the generate_month.php file in browser.

php generate months

In the above screen-shot, the function generate_months() generates 12 months from January to December.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *