2012-02-10 8 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の最初の日付とlastdateを取得する

私はそれをalittleに変更したいので、両方の関数に$ monthというパラメタがあります。$ monthは最初の日付と最後の日付を取得したい月の番号を保持します。

例私はfirstOfMonth(1)、私はそれが2012年1月1日を返したいんや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'); 

等々。

関連する問題