Not showing the current month???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Not showing the current month???

Post by cturner »

My code that is below is not showing the current month. Can someone please tell me why and how I can fix it? Thanks in advance.

Code: Select all

$now = time();
        $lastDay = date("t", $today);
        $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
        $thisDay = date("j");
        $thisMonth = date("M");
        $thisYear = date("Y");
             
        print "<select name=\"selectDay\" id=\"selectDay\">";
        for($i=1; $i<=$lastDay; $i++)
        {
            $selStr = $i == $thisDay ? " selected=\"selected\"" : "";
            print "<option value=\"$i\"$selStr>$i</option>";
        }
        print "</select>";

        print "<select name=\"selectMonth\" id=\"selectMonth\">";
        foreach($months as $v)
        {
            $selStr = $v == $thisMonth ? " selected=\"selected\"" : "";
            print "<option value=\"$v\"$selStr>$v</option>";
        }
        print "</select>";

        print "<select name=\"selectYear\" id=\"selectYear\">";
        for($i=$thisYear; $i<$thisYear+30; $i++)
        {
            $selStr = $i == $thisYear ? " selected=\"selected\"" : "";
            print "<option value=\"$i\"$selStr>$i</option>";
        }
        print "</select>";
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Problem solved.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Instead of simply posting 'problem solved' you could have posted the answer... So that people who end up with the same problem know what's going wrong..

Anyway, if i see it right the answer would be: Add a selected attribute to the select item of the current month...

Here is an example that doesn't use hardcoded month names... This way you can use it for other locales too :)

Code: Select all

<?php

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

function SelectMonths() {
        echo '<select name="selectMonth" id="selectMonth">';

        $current_month = date('n');

        for ($i = 1; $i < 13; ++$i) {
                $month_name = strftime("%B", mktime(0, 0, 0, $i, 1, 2006));

                echo '<option value="' . $i . '"';

                if ($i == $current_month) {
                        echo ' selected';
                }

                echo '>' . $month_name . '</option>';
        }

        echo '</select>';
}

setlocale(LC_TIME, 'C');
SelectMonths();
setlocale(LC_TIME, 'de_DE');
SelectMonths();

?>
Post Reply