如何在下拉列表中获取所有天、月和年?

发布于 2024-10-27 13:46:18 字数 90 浏览 0 评论 0原文

是否有一个类或 php 脚本会在所有天、月、年中在下拉字段中回显。我需要将它们用于出生日期字段。我用谷歌搜索,但没有发现任何我可以实施或学习的内容。有什么想法吗?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉月流沐 2024-11-03 13:46:18

如果您真的只想在 PHP 中完成,这里有一个简单的开始:

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

另一种方法,类似的输出:
(年份将按升序排列,而不是降序排列)

<?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();
?>

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

Another method, similar output:
(Years will be in Ascending order, rather than Descending)

<?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();
?>
赠我空喜 2024-11-03 13:46:18

为什么不使用一些基于 Javascript 的日历小部件,而不是使用三个不同的

例如,我多次听说过 jQuery Datepicker --效果要好得多,你不觉得吗:

       
(来源:pascal-martin.fr

(这只是我链接到的页面上演示的屏幕截图;您可以调整多个选项)

Instead of going with three distinct <select>, which is not a very user-friendly solution, why not use some Javascript-based calendar widget ?

For example, I've heard about jQuery Datepicker quite a few times -- the effect is far better, don't you think :

       
(source: pascal-martin.fr)

(And that's just a screenshot of the demo on the page I linked to ; there are several options you can tune)

暮倦 2024-11-03 13:46:18

该主题很旧,但会在接受的答案中说要添加

日期('M', mktime(0,0,0,$m))

-->;

日期('M', mktime(0,0,0,$m,1))

因为在 29 日、30 日和 31 日标签将是错误的。 PHP 将当前日期作为默认值,并且将返回缺少这一天的错误月份。 PHP 会将其解释为月份 + 1 天并显示下个月

The topic is old, but would say in the accepted answer to be added

date('M', mktime(0,0,0,$m))

-->

date('M', mktime(0,0,0,$m,1))

because on 29th, 30th and 31st the labels would be wrong. PHP takes as default the current day and will return wrong months in which this day is missing. PHP will interprete it as the month + 1 day and will show the following month

葵雨 2024-11-03 13:46:18

我知道这是一个老问题,但也许这会有所帮助。

var_dump(cal_info(CAL_GREGORIAN )['months']);

http://php.net/manual/en/function.cal-info。 php

http://php.net/manual/en/ref.calendar .php

I know that this is an old question but maybe this would help.

var_dump(cal_info(CAL_GREGORIAN )['months']);

http://php.net/manual/en/function.cal-info.php

http://php.net/manual/en/ref.calendar.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文