简体   繁体   English

从PHP中的当前月份开始的Selectbox月份和年份

[英]Selectbox Months and Years starting with current month in PHP

I'm hoping you guys can help me out with a PHP and Wordpress question. 我希望你们能为我解决PHP和Wordpress问题。 Currently I have 2 selectboxes, on from date and another to date thats lists both a month and year. 目前,我有2个selectboxes, 日期和另一个至今那同时列出了一月份和年份。 Currently the dates start at January of the current year, but i need them to start at the current month and year, then print for 2 years. 当前日期从当年的一月开始,但是我需要它们从当年的月和年开始,然后打印2年。

Here's the current code: 这是当前代码:

<div class="medium-4 small-6 columns">
  <label class="align-left">
    From:
    <?php
      $select_month_control = '<select name="from_date">';
      for($x = date("Y"); $x <= date("Y", strtotime('+1 year')); $x++) {
        for($y = 1; $y <= 12; $y++) {
          $month_val = $x.str_pad($y, 2, "0", STR_PAD_LEFT).'01';
          $select_month_control.= '<option value="' . $month_val . '" ';
          if ($from_date === $month_val) {
            $select_month_control .= 'selected="selected">';
          }
          else {
            $select_month_control .= '>';
          }
          $select_month_control.= date('F',mktime(0,0,0,$y,1,$year)). " " . $x .' </option>';
        }
      }
      $select_month_control.= '</select>';
      echo $select_month_control;
    ?>
  </label>
</div>
<div class="medium-4 small-6 columns">
  <label class="align-left">
    To:
    <?php
      $select_month_control = '<select name="to_date">';
      $max_x = date("Y", strtotime('+2 year'));
      for($x = date("Y"); $x <= $max_x; $x++) {
        for($y = 1; $y <= 12; $y++) {
          $month_val = $x.str_pad($y, 2, "0", STR_PAD_LEFT).'01';
          $select_month_control.= '<option value="' . $month_val . '" ';
          if ($to_date === $month_val) {
            $select_month_control .= 'selected="selected">';
          }
          else if (empty($to_date) && $x == $max_x && $y === 12) {
            $select_month_control .= 'selected="selected">';
          }
          else {
            $select_month_control .= '>';
          }
          $select_month_control.= date('F',mktime(0,0,0,($y + 1),0,$year)). " " . $x .' </option>';
        }
      }
      $select_month_control.= '</select>';
      echo $select_month_control;
    ?>
  </label>
</div>

So if the site was visited today the select box would start with December 2015 and end with December 2017 with all the months in between. 因此,如果今天访问了该站点,则选择框将从2015年12月开始,到2017年12月结束,介于这两个月之间。 It would also be super helpful if someone could point me in the right direction on how to change the To date to be the last day of the month as opposed to the first day of the month. 如果有人可以将正确的方向指向我,将“更改日期”更改为该月的第一天而不是该月的第一天,那么这也将非常有帮助。

Any thoughts would be appreciated. 任何想法将不胜感激。

Thanks so much! 非常感谢!

Maybe you can use a DateTime , a DateInterval and a DatePeriod to generate the dates for the select elements. 也许您可以使用DateTimeDateIntervalDatePeriod生成选择元素的日期。

For the DateInterval you can specify 1 month like this: DateInterval('P1M'); 对于DateInterval您可以这样指定1个月: DateInterval('P1M');

'P1M' stands for: “ P1M”代表:

P for "period P为“句号

1 for duration 1个持续时间

M for months M个月

http://php.net/manual/en/dateinterval.construct.php http://php.net/manual/en/dateinterval.construct.php

You can then create a DatePeriod and use a foreach loop to create the <option> elements. 然后,您可以创建一个DatePeriod并使用foreach循环创建<option>元素。

I have added a parameter $formatUseLastDayOfMonth to set the 'To' date to be the last day of the month. 我添加了一个参数$formatUseLastDayOfMonth以将“ To”日期设置为该月的最后一天。 This is an option which can be specified in the format method using the t option. 此选项可以使用t选项在format方法中指定。

From the documentation: 从文档中:

Use t for the number of days in the given month ( function.date ) t用作给定月份的天数( function.date

Because you use the select element 2 times, I have created a function which returns the select element as a string. 因为您使用了2次select元素,所以我创建了一个将select元素返回为字符串的函数。

For example: 例如:

<?php
/**
 * Create html select element which will contain options
 * for the given upcoming $numberOfMonths. The current month will be the default.
 *
 * @param int $numberOfMonths
 * @param int $selectedMonth
 * @param bool $formatUseLastDayOfMonth
 *
 * @return string
 */
function getSelectForMonths($numberOfMonths, $selectedMonth = 0, $formatUseLastDayOfMonth = false)
{
    // Add 1 month extra because the $datePeriod we are about to loop excludes the endDate
    $numberOfMonths+=1;
    $dateFormat = $formatUseLastDayOfMonth ? "Ymt" : "Ym01";
    $end = new DateTime();
    $end->modify(sprintf("+%s month", $numberOfMonths));

    $datePeriod = new DatePeriod(
        new DateTime(),
        new DateInterval('P1M'),
        $end
    );

    $selectMonthControl = '<select name="from_date">';
    $monthCounter = 0;
    foreach ($datePeriod as $date) {
        $selectMonthControl .= sprintf(
            "<option value='%s'%s>%s</option>",
            $date->format($dateFormat),
            $selectedMonth === $monthCounter ? 'selected="selected"' : '',
            $date->format('F Y')
        );
        $monthCounter++;
    }
    $selectMonthControl .= "</select>";

    return $selectMonthControl;
}
?>

And then you can use it like this: 然后您可以像这样使用它:

<div class="medium-4 small-6 columns">
    <label class="align-left">
        From:
        <?php echo getSelectForMonths(24); ?>
    </label>
</div>
<div class="medium-4 small-6 columns">
    <label class="align-left">
        To:
        <?php echo getSelectForMonths(24, 24, true); ?>
    </label>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM