2012-02-10 11 views
0
function firstOfMonth() { 
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00')); 
} 

function lastOfMonth() { 
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))); 
} 

이 달의 첫날 날짜와 마지막 날 날짜를 가져와야합니다.PHP에서 첫 번째 특정 날짜와 마지막 날짜를 잡는 중

이제 alittle을 변경하고 싶습니다. 두 함수 모두에 $ month 매개 변수가 있습니다. $ month에는 첫 번째 날짜와 마지막 날짜를 얻고 자하는 달의 숫자가 저장됩니다.

내가 firstOfMonth (1), 나는 그것이 2012-01-01을 반환하고 lastOfMonth (1)가 반환해야 원하는 경우

예 2012-01-31

내가 어떻게 할 수 있습니까?

+0

[ 한 달의 첫날과 마지막 날을 얻는 법] (http://stackoverflow.com/questions/7541938/how-to-get-the-first-and-last-days-of-a-give-month) – deceze

답변

0

이 시도 :

$date = new DateTime('2012-02-01'); 

// first day 
echo $date->format('d-m-Y'); 

// t = days in month, minutes one to go to the last. 
$date->modify(sprintf('+%d day', $date->format('t')-1)); 

// Last day 
echo $date->format('d-m-Y'); 
0

당신이 5.3+ PHP를 사용하는 경우,이 시도,

$query_date = '2012-01-01'; 
$date = new DateTime($query_date); 
//First day of month 
$date->modify('first day of this month'); 
$firstday= $date->format('Y-m-d'); 
//Last day of month 
$date->modify('last day of this month'); 
$lastday= $date->format('Y-m-d'); 

을 다음 달 마지막 날짜를 찾는를 들어, 다음과 같이 수정

$date->modify('last day of 1 month'); 
echo $date->format('Y-m-d'); 

등등.

관련 문제