在PHP中获得前几个月
I need to display 3 month from the previous month in php
Here is what I have tried so far
<select style='width: 112px;' name='PayMonth'>
<option name="PayMonth" value=''>Select Month</option>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i; ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
</option>
<?php
}?>
This will display all months
But how can I display on 3 months from current month
i.e.,
If current month is april it should show Feb, Mar, Apr
If current month is jan it should show nov, dec, jan
I tried..
<?php
$month = date("m");
?>
But in the loop I am confused with
for($i = 1 ; $i <= $month; $i++)
我需要在上个月显示3个月的php p>
这里 是我到目前为止尝试过的 p>
&lt; select style ='width:112px;' name ='PayMonth'&gt;
&lt; option name =“PayMonth”value =''&gt;选择月&lt; /选项&gt;
&lt;?php
for($ i = 1; $ i&lt; = 12; $ i ++)
{
$ allmonth = date(“F”,mktime(0,0,0,$ i,1, date(“Y”)))
?&gt;
&lt; option value =“&lt;?php echo $ i;?&gt;” &gt;&lt;?php echo date(“F”,mktime(0,0,0,$ i,1,date(“Y”)));?&gt;
&lt; / option&gt;
&lt;?php \ n}?&gt;
code> pre>
这将显示所有月份 p>
但是如何显示当月的3个月
即, p>
如果当前月份是4月,则应显示2月,3月,4月 strong> p>
\ n 如果当前月份是jan,它应该显示nov,dec,jan strong> p>
我试过.. p>
&lt;?php
$ month = date(“m”);
?&gt;
code> pre>
但在循环中我 与 p>
混淆($ i = 1; $ i&lt; = $ month; $ i ++)
code> pre>
div >
If you are getting $month = date("m");
Then your forloop should be
for($i = $month-3 ; $i <= $month; $i++)
Sidenote :
As you are using
<option value="<?php echo $i; ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
You may get -1, -2 inside the values. So you should change
<option value="<?php echo date("m",mktime(0,0,0,$i,1,date("Y"))); ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
To get
<select style='width: 112px;' name='PayMonth'>
<option name="PayMonth" value=''>Select Month</option>
<option value="11" >November </option>
<option value="12" >December </option>
<option value="01" >January </option>
<option value="02" >February </option>
</select>
Just do:
date('m', strtotime('-1 month'));
to get last month.
You can put -2 or -3 to get 2 months ago or 3 months ago also.
Substitute 'm' with the format of your choice.
Easy as cake:
echo date('Y-m-d', strtotime("first day of -1 month"));
And you can substitute -1 with the number of your choice:
<?php
foreach(range(-1,-3,-1) as $value) {
echo date('Y-m-d', strtotime(sprintf("first day of %d month", $value)));
}
Try this..
echo date('M', strtotime('0 month'));
echo date('M', strtotime('-1 month'));
echo date('M', strtotime('-2 month'));
echo date('M', strtotime('-3 month'));
Take into consideration that you will get wrong results when using months back using the current day - on every 31st day the month.
Try this instead:
<?
$monthsBack = 3;
// we need to do set day to middle of the month,
// otherwise "-x month" will return wrong results on the 31st of each month:
$now = mktime(0, 0, 0, date("m"), 15, date("Y"));
for ($i1=0; $i1<$monthsBack; $i1++) {
$displayDate = strtotime("-{$monthsBack} month", $now);
echo '<option value="'.date("m", $displayDate).'" >'.date("F", $displayDate).'</option>';
}
?>
// Today is 2015-02-30
echo date('Y-m-d', strtotime('last month'));