user3150060
user3150060

Reputation: 1745

drop down list of Month and Day for birthday in PHP

What's the best way to get the Months and Days drop down list in PHP ?

I tried the following code, but mktime is depreciated :

 for ($j = 1; $j <= 12; $j++) 
 {
    $month_text = date("F", time(0, 0, 0, $j+1, 0, 0, 0));
    $month[$j] = $month_text;
 }  

Thanks.

Upvotes: 0

Views: 1666

Answers (2)

S. Dixon
S. Dixon

Reputation: 841

This question has been asked before here: How to get all days, months, and years in a drop down?

This is what he did, but keep in mind that he did use mktime which as you say is deprecated. To avoid making a list for year, do this:

<?php

    // build months menu
    echo '<select name="month">' . PHP_EOL;
    for ($m=1; $m<=12; $m++) {
        echo '  <option value="' . $m . '">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
    }
    echo '</select>' . PHP_EOL;

    // build days menu
    echo '<select name="day">' . PHP_EOL;
    for ($d=1; $d<=31; $d++) {
        echo '  <option value="' . $d . '">' . $d . '</option>' . PHP_EOL;
    }
    echo '</select>' . PHP_EOL;
?>

Upvotes: 1

John Conde
John Conde

Reputation: 219844

You're making it much more complex than it needs to be. Just put the names of the months in an array and use range() to create the array of days.

$months = array("January","February","March","April","May","June","July","August","September","October","November","December");
$days = range(1, 31);

If you want the month number to coincide with the month name just add an empty array element to the beginning of the month array.

Upvotes: 3

Related Questions