如何获得所有的天,月和年在下拉? [英] How to get all days, months, and years in a drop down?

查看:166
本文介绍了如何获得所有的天,月和年在下拉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个类或一个php脚本,将在下拉字段中回显所有的天,月和年。我需要使用它们的出生日期字段。我googled,但没有出现,我可以实现或学习。有任何想法吗?感谢

Is there a class or a php script that will echo in drop down fields all the days, months, and years. I need to use them for date of birth fields. I googled but nothing came up that i can implement or learn from. ANy ideas? thanks

推荐答案

如果你真的只是想在PHP中完成, p>

If you REALLY just want it done in PHP, here's a simple start:

<?php
    // lowest year wanted
    $cutoff = 1910;

    // current year
    $now = date('Y');

    // build years menu
    echo '<select name="year">' . PHP_EOL;
    for ($y=$now; $y>=$cutoff; $y--) {
        echo '  <option value="' . $y . '">' . $y . '</option>' . PHP_EOL;
    }
    echo '</select>' . PHP_EOL;

    // 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;
?>






另一种方法, / strong>

(年数将按升序而不是降序)

<?php
    $build = array(
        array('year', '1910', date('Y'), 'Y'),
        array('month', '1', '12', 'M'),
        array('day', '1', '31', 'j')
    );
    $doc = new DOMDocument();
    foreach ($build as $item) {
        $menu = $doc->createElement('select');
        $menu->setAttribute('name', $item[0]);
        for ($x=$item[1]; $x<=$item[2]; $x++) {
            $b = $item[3];
            $opt = $doc->createElement('option');
            $opt->setAttribute('value', $x);
            $opt->nodeValue = date($item[3], mktime(0,0,0,($b=='M'?$x:1),($b=='j'?$x:1),($b=='Y'?$x:1)));
            $menu->appendChild($opt);
        }
        $doc->appendChild($menu);
    }
    echo $doc->saveHTML();
?>

这篇关于如何获得所有的天,月和年在下拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆