2016-07-31 4 views
-1

양식에서 해당 연도를 제출하면 특정 연도의 달 목록을 가져오고 싶습니다. 개월은 시작 날짜와 그 달의 마지막 날짜와 함께 나와야합니다.시작 날짜와 마지막 날짜가있는 특정 연도의 월 목록을 가져옵니다.

$year = $_POST['year']; 
$current_year = date('Y'); 

for ($m=1;$m<=12;$m++) 
{$month = date("F", mktime(0,0,0,$m,1,$year)); 
$nmonth = date("m",strtotime($month)); 
$days = cal_days_in_month(CAL_GREGORIAN,$nmonth,$year); 
$sdate = mktime(0, 0, 0, $nmonth, 1, $year); 
$edate = mktime(0, 0, 0, $nmonth, $days, $year); 
//$edate = date("Y-m-t", strtotime($sdate)); 
echo strftime("$month $year").' - '.'Start date '.''.strftime(" %Y-%m-%d",  $sdate).' '.'End date '.''.$edate.' - '.strtoupper(substr($month, 0, 3)).date("y",mktime(0,0,0,$nmonth,1,$year)).$days.'<br>'; 
//echo $month.' '.$days.' '.$sdate.' '.$edate.'<br>'; 
} 

<body> 
<form id="period" name="period" method="post" action="create_period.php"> 

    <input type="text" name="year" id="year" /> 

    <label> 
    <input type="submit" name="button" id="submit" value="Create Period" /> 
    </label> 
    <input type="hidden" name="id" id="id" /> 
</form> 

</body> 

답변

2

이 같은 DatePeriod를 사용할 수 있습니다

$year = 2015; 

$period = new DatePeriod(
    (new DateTime())->setDate($year, 1, 1), //Start at 2015-01-01 
    new DateInterval('P1M'), //Step is 1 month 
    (new DateTime())->setDate($year+1, 1, 1) //End at 2016-01-01 
); 

foreach($period as $date){ 
    $firstDay = $date->modify('first day of this month')->format("Y/m/d") ; 
    $lastDay = $date->modify('last day of this month')->format("Y/m/d") ; 

    echo "$firstDay - $lastDay <br>"; 
} 

데모 :https://3v4l.org/CidUa

+0

고맙습니다 @의 이스마일 - rbouh을. 당신은 천재입니다 – user3315848

+0

내가 도와 주셔서 감사합니다. [link] (http://stackoverflow.com/questions/38684317/values-fail-to-insert-when-into-table-fields-when-posted)에서 또 다른 문제가 있습니다. – user3315848

관련 문제