PHP 当前年份的日期函数无法正常工作?
好吧,我试图让一个表单下拉菜单自动选择当前日期,而无需太多的 javascript 编码,所以我得到了下面的代码(它不会自动选择,但它确实包含当前日期作为第一个选项)。这一年并没有像预期的那样首先出现。我对日和月做了同样的事情,这两个工作完美,但是当我有相同的代码时,除了日/月代替年(括号内的 mday 和 mon),第一年选择没有出现。我猜问题出在 [$t['year']] 的两个实例内的部分。代码的其余部分运行良好,但我只是将其包含在内,以防万一我错过了一些内容(我也尝试将所有年份替换为最后的各自的最后两位数字,但结果相同)。
<?php
$t = getdate(time());
$year = array(1 =>'2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030');
echo '  Year <select name="year" >';
echo '<option value="\" . $t[\'year\'] . \"">' . $year[$t['year']] . '</option>';
foreach( $year as $key => $value ) {
echo "<option value = \"$key\">$value</option>";
}
echo '</select>'; ?>
Ok so I was trying to have a form dropdown menu autoselect the current date without too much javascript coding, so I got the code below (it doesn't autoselect, but it does include the current date as the first options). The year wasn't showing up first like it was supposed to. I did the same for day and month, and those 2 worked perfectly, yet when I had the same code, except days / months in place of years (mday and mon inside brackets), the first year selection wasn't appearing. I'm guessing the problem is the part inside two instances of [$t['year']]. The rest of the code is functioning fine, but I just included it in case I missed something about it (I also tried replacing all years with the last their respective last 2 digits but same result).
<?php
$t = getdate(time());
$year = array(1 =>'2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030');
echo '  Year <select name="year" >';
echo '<option value="\" . $t[\'year\'] . \"">' . $year[$t['year']] . '</option>';
foreach( $year as $key => $value ) {
echo "<option value = \"$key\">$value</option>";
}
echo '</select>'; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
日期('Y')
。http://php.net/manual/en/function.date.php
http://codepad.org/7h2MnxzC
这也将仅选择
select,所以你不需要二:
http://codepad.org/uGx6zoyn
Use
date('Y')
.http://php.net/manual/en/function.date.php
http://codepad.org/7h2MnxzC
This will also select just the one value in the
select
, so you don't need two:http://codepad.org/uGx6zoyn