开发者

PHP loop through months array

This should be easy but I'm having t开发者_StackOverflowrouble...

In PHP how can I echo out a select drop down box that defaults to the current month and has options for 8 months prior (even if it goes in the last year).

For example, for this month it would default to June and end at November.


$months = array();
for ($i = 0; $i < 8; $i++) {
    $timestamp = mktime(0, 0, 0, date('n') - $i, 1);
    $months[date('n', $timestamp)] = date('F', $timestamp);
}

Alternative for "custom" month names:

$months = array(1 => 'Jan.', 2 => 'Feb.', 3 => 'Mar.', 4 => 'Apr.', 5 => 'May', 6 => 'Jun.', 7 => 'Jul.', 8 => 'Aug.', 9 => 'Sep.', 10 => 'Oct.', 11 => 'Nov.', 12 => 'Dec.');
$transposed = array_slice($months, date('n'), 12, true) + array_slice($months, 0, date('n'), true);
$last8 = array_reverse(array_slice($transposed, -8, 12, true), true);

To output an array of such months as dropdown is as simple as:

<select name="month">
    <?php
        foreach ($months as $num => $name) {
            printf('<option value="%u">%s</option>', $num, $name);
        }
    ?>
</select>


If you need to print like select box options, try using this:

for($i=1;$i<13;$i++)
print("<option>".date('F',strtotime('01.'.$i.'.2001'))."</option>");


$months = array(
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July ',
    'August',
    'September',
    'October',
    'November',
    'December',
);

$current = date('F');
$start = array_search($current, $months);

$toshow = array();
$total = 0;
for($i = $start; $total < 8; $i--)
{
    if($i == 0)
    {
        $i = 12;
    }

    $toshow[] = $months[$i];
    $total++;
}

var_dump($toshow);

Give that a shot, just dump out $toshow to your HTML.


Another simple process to Get month names using a for loop and PHP date function.

<?php
   for($m=1; $m<=12; ++$m){
     echo date('F', mktime(0, 0, 0, $m, 1)).'<br>';
}


This is absolutely based on deceze's answer only months are sorted backwards and current month is selected

  $curr_month = date('F',mktime(0, 0, 0, date('n')));
  $months = array();

 for ($i = 1; $i <= 8; $i++) {
 $months[] = date('F', mktime(0, 0, 0, date('n') - $i, 1));
 }
  $months = array_reverse($months, true);

echo "<select>\n";
foreach($months as $key =>$value){
       echo "<option value='$value'>$value</option>\n";
                                  }

 echo "<option value='$curr_month' selected='selected'>$curr_month</option>
  </select>";


some functions below... on page load will select current day, month, year + includes your required -8 months

<form method="POST" action="">
<p><select size="1" name="day">
<?php formDay(); ?>
</select>-
<select size="1" name="month">
<?php formMonth(); ?>
</select>-
<select size="1" name="year">
<?php formYear(); ?>
</select> <input type="submit" value="Submit"></p>
</form>

<?php
//functions to loop day,month,year
function formDay(){
    for($i=1; $i<=31; $i++){
        $selected = ($i==date('n'))? ' selected' :'';
        echo '<option'.$selected.' value="'.$i.'">'.$i.'</option>'."\n";
    }
}
//with the -8/+8 month, meaning june is center month
function formMonth(){
    $month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 8 months');
    $end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 8 months');
    while($month < $end){
        $selected = (date('F', $month)==date('F'))? ' selected' :'';
        echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
        $month = strtotime("+1 month", $month);
    }
}

function formYear(){
    for($i=1980; $i<=date('Y'); $i++){
        $selected = ($i==date('Y'))? ' selected' :'';
        echo '<option'.$selected.' value="'.$i.'">'.$i.'</option>'."\n";
    }
}
?>


If you need to set this January to current month then this is how:

    <?php 

    $startMonth = date('1');
    $endtMonth = date('m');
    for($m=$endtMonth ; $m<=$currentMonth; ++$m){
        $months[$m] = strftime('%B', mktime(0, 0, 0, $m, 1));
        echo $months[$m]."<br>";
    }

 ?>


You can use this function. I added ksort to order the array January-December without depending on luck

function months($month_format="F"){
    $months =  [];
    for ($i = 0; $i < 12; $i++) {
        $timestamp = mktime(0, 0, 0, date('n') - $i, 1);
        $months[date('n', $timestamp)] = date($month_format, $timestamp);
    }
    ksort($months,SORT_NUMERIC);
    return $months;
}


mkdate is used before but it can be done with 4 arguments instead of 5.

<select >
  <option value="">select month</option>
   
   <?php
     for($m=1; $m<=12; $m++){
       echo '<option value="'.$m.'">'.date('F', mktime(0, 0, 0, $m)).'</option>';
     }
   ?>
</select>



0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜