【问题标题】:How to add 2014 in my date function - PHP? [closed]如何在我的日期函数中添加 2014 - PHP? [关闭]
【发布时间】:2013-10-03 09:46:46
【问题描述】:

我不久前发现这个日期功能可以正常工作(不记得我在哪里找到的),但它显示 2013 年以来的所有日期、月份和年份。问题是现在我需要人们能够选择 2014从下拉列表中也是如此。不太确定我应该编辑什么来添加它。

感谢您的帮助...代码:

function date_dropdown($year_limit = 0){

   $daynow = date("d");
   $monthnowtxt = date('F');
   $monthnow = date('m');
   $yearnow = date("20y");

   $html_output = '<div id="date_select" >'."\n";
   $html_output .= '<label for="date_day">Date of Longplay</label>'."\n";

    /*days*/
   $html_output .= '<select name="date_day" id="day_select"><option  ="selected" value="' . $daynow . '">' . $daynow . '</option>'."\n";
   for ($day = 1; $day <= 31; $day++) {
       $html_output .= '<option>' . $day . '</option>'."\n";
   }
  $html_output .= '</select>'."\n";

    /*months*/
  $html_output .= '<select name="date_month" id="month_select" ><option selected="selected" value="' . $monthnow . '">' . $monthnowtxt . '</option>'."\n";
  $months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  for ($month = 1; $month <= 12; $month++) {
      $html_output .= '               <option value="' . $month . '">' . $months[$month] . '</option>'."\n";
   } 
   $html_output .= '</select>'."\n";

   /*years*/
   $html_output .= '<select name="date_year" id="year_select"><option ="selected" value="' . $yearnow . '">' . $yearnow . '</option>'."\n";
   for ($year = 1900; $year <= (date("Y") - $year_limit); $year++) {
           $html_output .= '               <option>' . $year . '</option>'."\n";
   }
   $html_output .= '</select>'."\n";

   $html_output .= '</div>'."\n";
   return $html_output;
  }

【问题讨论】:

  • “不太确定我应该编辑什么来添加它。” 好吧,我看到变量$year 上的for 循环打印&lt;option&gt; 标签。这可能与它有关。
  • 现在,如果您像这样构建它并更专注于可读的后端代码,则只需忽略漂亮的 HTML 生成。如果有人真的想阅读生成的源代码(包括您),那里有很多漂亮的打印机。或者更好的是,使用模板或其他东西,而不是直接连接 HTML 字符串。我的 2c。
  • @plain-jane:如果它是您自己的代码,您对此代码的编辑会很好 - 我同意空格是不必要的。但最好将代码大部分保留在其自然状态,否则存在丢失对问题至关重要的东西的危险。 (现在已经获得批准,所以在这种情况下不用担心 - 只是对未来编辑的建议)。
  • @halfer:是的,我知道我们应该respect the author,但只是为了减少水平滚动和更好的可读性我编辑了它,没有其他意图

标签: php date


【解决方案1】:

这个方法调用给你2014:

date_dropdown(-1);

【讨论】:

  • 真的,错过了
【解决方案2】:

这是你需要改变的一点

for ($year = 1900; $year <= (date("Y") - $year_limit); $year++)

date('Y') 将返回 2013

date('Y')+1 将返回 2014

for ($year = 1900; $year <= ((date("Y")+1) - $year_limit); $year++)

我不得不说我不喜欢你包含的功能,并且可能有更干净更灵活的方法来完成它的工作。

【讨论】:

  • 谢谢 allen213.. 没想到这么简单。真正的快速帮助。非常感谢
  • 亚当斯的答案更简单
  • 哈,我在写答案时接了电话……你打败了我:(哈哈
  • @user1936399 - 为什么不使用我的解决方案?无需修改功能代码:)
  • 为了它的价值@Adam 我会用你的
【解决方案3】:

改变这个

for ($year = 1900; $year <= (date("Y") - $year_limit); $year++)

到这里

for ($year = 1900; $year <= ((date("Y")+1) - $year_limit); $year++)

【讨论】:

    【解决方案4】:

    更改您的 for 循环

    for ($year = 1900; $year <= (date("Y") - $year_limit); $year++)
    

    for ($year = 1900; $year <= $year_limit); $year++)
    

    并传递大于 2014 的参数...

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2012-10-08
      • 2013-12-17
      • 2020-10-06
      • 1970-01-01
      • 2011-02-20
      相关资源
      最近更新 更多