PHP 当前年份的日期函数无法正常工作?

发布于 2024-12-09 11:29:04 字数 892 浏览 0 评论 0原文

好吧,我试图让一个表单下拉菜单自动选择当前日期,而无需太多的 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 '&nbsp 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 技术交流群。

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

发布评论

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

评论(2

千仐 2024-12-16 11:29:04

使用日期('Y')

http://php.net/manual/en/function.date.php

<?php
$t = getdate(time()); 
$year = array('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" >'."\n"; 
echo '<option value="' . array_search(date('Y'),$year) . '">' . date('Y') . '</option>'."\n"; 
foreach( $year as $key => $value ) { 
    echo "<option value = \"$key\">$value</option>\n"; 
} 
echo '</select>'."\n";

?>

http://codepad.org/7h2MnxzC

这也将仅选择 select,所以你不需要二:

<?php
$t = getdate(time()); 
$year = array('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'); 
$c_year = date('Y');

echo '  Year <select name="year" >'."\n"; 
foreach( $year as $key => $value ) { 
    echo "<option".($c_year == $value?' selected="true"':'')." value = \"$key\">$value</option>\n"; 
} 
echo '</select>'."\n";

?>

http://codepad.org/uGx6zoyn

Use date('Y').

http://php.net/manual/en/function.date.php

<?php
$t = getdate(time()); 
$year = array('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" >'."\n"; 
echo '<option value="' . array_search(date('Y'),$year) . '">' . date('Y') . '</option>'."\n"; 
foreach( $year as $key => $value ) { 
    echo "<option value = \"$key\">$value</option>\n"; 
} 
echo '</select>'."\n";

?>

http://codepad.org/7h2MnxzC

This will also select just the one value in the select, so you don't need two:

<?php
$t = getdate(time()); 
$year = array('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'); 
$c_year = date('Y');

echo '  Year <select name="year" >'."\n"; 
foreach( $year as $key => $value ) { 
    echo "<option".($c_year == $value?' selected="true"':'')." value = \"$key\">$value</option>\n"; 
} 
echo '</select>'."\n";

?>

http://codepad.org/uGx6zoyn

戴着白色围巾的女孩 2024-12-16 11:29:04
$years = range(2000, 2030);
$cur_year = date('Y');

echo '<select name="year">';
foreach($years as $year)
    if($year == $cur_year)
        echo "<option selected>$year</option>"; 
    else
        echo "<option>$year</option>";
echo '</select>';
$years = range(2000, 2030);
$cur_year = date('Y');

echo '<select name="year">';
foreach($years as $year)
    if($year == $cur_year)
        echo "<option selected>$year</option>"; 
    else
        echo "<option>$year</option>";
echo '</select>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文