dropdown month and year in php

php-sniplets Add comments

Generate a dropdown with days of the month and a dropdown with months in a year

The below code will generate a form drop-down for all the days of the month, with today’s day selected.
Convenient for those date selectors!

****************************
<select name=”start_day”>

<?php
global $LOC;

$current_time_day = $LOC->decode_date(‘%d’, $LOC->now);

for ($i = 1; $i <= 31; $i++) {
echo “<option value=’$i’”;

if ($i == $current_time_day) { echo ” selected=’selected’”; }

echo “>$i</option>
“;
} ?>

</select>

*************************************

And here the code for the dropdown with months in a year

The below code will generate a dropdown for all twelve months, with the actual month selected. Convenient for
those date choosers!

********************************************

<select name=”start_month”>

<?php
global $LOC;

$current_time_m = $LOC->decode_date(‘%m’, $LOC->now);

for ($i = 1; $i <= 12; $i++) {
echo “<option value=’$i-’”;

if ($i == $current_time_m) { echo ” selected=’selected’”; }

$month_text = date(“F”, mktime(0, 0, 0, $i+1, 0, 0, 0));
echo “>$month_text</option>
“; } ?>

</select>

Leave a Reply

You must be logged in to post a comment.

Impressum Entries RSS Log in