dongshou1856 2011-10-11 21:32
浏览 15
已采纳

当前年份的PHP日期函数不能正常工作?

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 '&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>'; ?>
  • 写回答

2条回答 默认 最新

  • douling0053 2011-10-11 21:35
    关注

    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 '&nbsp Year <select name="year" >'."
    "; 
    echo '<option value="' . array_search(date('Y'),$year) . '">' . date('Y') . '</option>'."
    "; 
    foreach( $year as $key => $value ) { 
        echo "<option value = \"$key\">$value</option>
    "; 
    } 
    echo '</select>'."
    ";
    
    ?>
    

    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 '&nbsp Year <select name="year" >'."
    "; 
    foreach( $year as $key => $value ) { 
        echo "<option".($c_year == $value?' selected="true"':'')." value = \"$key\">$value</option>
    "; 
    } 
    echo '</select>'."
    ";
    
    ?>
    

    http://codepad.org/uGx6zoyn

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)