简体   繁体   English

PHP Select 选项从当前月份返回 2 个月前

[英]PHP Select option return 2 months ago from current month

Hi I would like to know.嗨,我想知道。

how do I get two months from current month Allocated with 1-15 and 16 to 31如何从当前月份获得两个月分配 1-15 和 16 到 31

I need output something like this:我需要 output 是这样的:

<option value="2014-09-15">September 15, 2014</option>
<option value="2014-09-30">September 30, 2014</option>
<option value="2014-10-15">October 15, 2014</option>
<option value="2014-10-30">October 30, 2014</option>

I have sample code but its output 12 months我有示例代码,但它的 output 12 个月

<?php

for ($i = 0; $i <= 11; ++$i) 
{
$time = strtotime(sprintf('+%d months', $i));
$value = date('m', $time);
$label = date('F', $time);

//if month is set stay on that month

if($month==$value)
{ printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
}
else
{printf('<option value="%s">%s</option>', $value, $label);}
}

?>

For example:例如:

$currentMonth = date("n");
for($i=1;$i<3;$i++) {
    $monthNum = $currentMonth - $i;
    if($monthNum<1)
        $monthNum = 12 - $monthNum;
    $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
    //here use $monthNum and $monthName for output
    print "<option>......</option>";
}

Use function date, which is more easy and spares you the loop...使用 function 日期,更简单,省去你的循环...

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

Some theory first:先说一些理论:

$currentMonth = date("t", time());

This returns wether 31, 30 or 28/29 depending on month given in time parameter.这将根据时间参数中给出的月份返回 31、30 或 28/29。 time() returns a unix-timestamp that is without any parameter just "now" so its the current month. time() 返回一个没有任何参数的 unix-timestamp,只是“现在”,所以它是当前月份。

If we want the amount of days from the next month do the same again just adding seconds to time(), that is enough to reach the next month... so 32 days will in any case bring you to the exact next month...如果我们希望从下个月开始的天数再次执行相同的操作,只需在 time() 中添加秒数,就足以到达下个月……所以 32 天无论如何都会带你到下个月。 .

Sidenote: 1 hour has 3600 seconds * 24 = 1 day has 86400.旁注:1 小时有 3600 秒 * 24 = 1 天有 86400。

$nextMonth = date("t", time() + (86400 * 32)); //+32 days will guarantee next month

Now I present you the entire solution, pretty much means your entire output can be generated like this:现在我向您展示整个解决方案,这几乎意味着您的整个 output 可以像这样生成:

$timeNow = time();
$timeNextMonth = $timeNow + (86400 * 32); //+ 32 days

echo '
    <select name="date">
        <option value="' . date("Y-m-15", $timeNow) . '">' . date("F 15, Y", $timeNow) . '</option>
        <option value="' . date("Y-m-t", $timeNow) . '">' . date("F t, Y", $timeNow) . '</option>
        <option value="' . date("Y-m-15", $timeNextMonth) . '">' . date("F 15, Y", $timeNextMonth) . '</option>
        <option value="' . date("Y-m-t", $timeNextMonth) . '">' . date("F t, Y", $timeNextMonth) . '</option>
    </select>
';

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

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