web-dev-qa-db-ja.com

日付から月の最終日を見つける方法は?

PHPで月の末日を取得する方法を教えてください。

与えられた:

$a_date = "2009-11-23"

私は2009-11-30が欲しいです。そして与えられた

$a_date = "2009-12-23"

2009-12-31が欲しいです。

337

tは、指定された日付の月の日数を返します( date のドキュメントを参照)。

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));
718
Dominic Rodger

Strtotime()を使用したコードは、2038年以降は失敗します。(このスレッドの最初の回答にあるとおり)たとえば、次のようにして試してください。

$a_date = "2040-11-23";
echo date("Y-m-t", strtotime($a_date));

それは答えを与える:1970-01-31

だからstrtotimeの代わりに、DateTime関数を使うべきです。次のコードは2038年問題なく動作します。

$d = new DateTime( '2040-11-23' ); 
echo $d->format( 'Y-m-t' );
100
Mugunth

私はこれが少し遅れているのを知っています、しかし私は DateTime クラスを使うことによってPHP 5.3+でこれをするもっとエレガントな方法があると思います:

$date = new DateTime('now');
$date->modify('last day of this month');
echo $date->format('Y-m-d');
84
Hannoun Yassir

組み込みのPHP関数cal_days_in_month()?もあります。

msgstr "この関数は指定されたカレンダーの年の月の日数を返します。" http://php.net/manual/en/function.cal-days-in-month

echo cal_days_in_month(CAL_GREGORIAN, 11, 2009); 
// = 30
63
MPV

これはうまくいくはずです。

$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());

$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());

$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());

echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';

echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';

echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
24
kaleazy

翌月の最初の日の日付を作成してから、strtotime("-1 day", $firstOfNextMonth)を使用できます。

17
nikc.org

PHP 5.3以降を使用している場合は、これを試してください。

$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');

翌月の最終日を見つけるには、次のように修正します。

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

等々..

11
Mohammed Safeer

何が悪いのか - 私にとって最もエレガントなのは DateTime を使うことです

DateTime::createFromFormat 、ワンライナーが表示されないのでしょうか。

$lastDay = \DateTime::createFromFormat("Y-m-d", "2009-11-23")->format("Y-m-t");
11
john Smith

あなたの解決策はここにあります。

$lastday = date('t',strtotime('today'));
9
Vineet Kadkol

PHP DateTimeに Carbon API拡張を使用する場合は、次のようにして月の最後の日を取得できます。

$date = Carbon::now();
$date->addMonth();
$date->day = 0;
echo $date->toDateString(); // use toDateTimeString() to get date and time 
7
eaykin

あなたはその月の最後の日をいくつかの方法で見つけることができます。しかし、これをPHP strtotime() および date() functionを使用して簡単に実行できます。

$a_date = "2009-11-23";
echo date('Y-m-t',strtotime($a_date));

ライブデモ

しかし、あなたがPHP> = 5.2を使っているなら、私はあなたが新しいDateTimeオブジェクトを使うことを強く勧めます。例えば以下のようになります。

$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');

ライブデモ

また、以下のように独自の関数を使用してこれを解決することができます。

/**
 * Last date of a month of a year
 *
 * @param[in] $date - Integer. Default = Current Month
 *
 * @return Last date of the month and year in yyyy-mm-dd format
 */
function last_day_of_the_month($date = '')
{
    $month  = date('m', strtotime($date));
    $year   = date('Y', strtotime($date));
    $result = strtotime("{$year}-{$month}-01");
    $result = strtotime('-1 second', strtotime('+1 month', $result));

    return date('Y-m-d', $result);
}

$a_date = "2009-11-23";
echo last_day_of_the_month($a_date);
4
Faisal

Datetimeと一緒に使うこともできます

$date = new \DateTime();
$nbrDay = $date->format('t');
$lastDay = $date->format('Y-m-t');
4
Ajouve

Zend_Dateを使うのはとても簡単です。

$date->setDay($date->get(Zend_Date::MONTH_DAYS));
2
mkerstner
$date1 = $year.'-'.$month; 
$d = date_create_from_format('Y-m',$date1); 
$last_day = date_format($d, 't');
2
kayla

これは完全な関数です:

public function get_number_of_days_in_month($month, $year) {
    // Using first day of the month, it doesn't really matter
    $date = $year."-".$month."-1";
    return date("t", strtotime($date));
}

これは次のように出力されます。

echo get_number_of_days_in_month(2,2014);

出力:28

1
Firze

私は遅れていますが、これを行うための簡単な方法がいくつかあります。

$days = date("t");
$days = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
$days = date("j",mktime (date("H"),date("i"),date("s"),(date("n")+1),0,date("Y")));

Mktime()を使用して、時間のあらゆる側面を完全に制御します。I.E.

echo "<br> ".date("Y-n-j",mktime (date("H"),date("i"),date("s"),(11+1),0,2009));

日を0に設定して月を1つ上げると、前月の最後の日になります。 0と負の数は、異なる引数で同様の効果があります。 PHP:mktime - マニュアル

ごく一部の人が言っているように、strtotimeは最も堅実な方法ではないし、誰もが同じくらい簡単には多目的に使えるものでないとしてもほとんどありません。

1
JSG

月末を取得する方法があります。

//to get last day of current month
echo date("t", strtotime('now'));

//to get last day from specific date
$date = "2014-07-24";
echo date("t", strtotime($date));

//to get last day from specific date by calendar
$date = "2014-07-24";
$dateArr=explode('-',$date);
echo cal_days_in_month(CAL_GREGORIAN, $dateArr[1], $dateArr[0]); 
1
vineet

賢い月があれば、その月の最終日を取得してください。

public function getLastDateOfMonth($month)
    {
        $date = date('Y').'-'.$month.'-01';  //make date of month 
        return date('t', strtotime($date)); 
    }

$this->getLastDateOfMonth(01); //31
0

Carbon [PHP DateTimeのAPI拡張

Carbon::parse("2009-11-23")->lastOfMonth()->day;

または

Carbon::createFromDate(2009, 11, 23)->lastOfMonth()->day;

再試行します

30
0
josef

mktime を使用し、date( 't')を使用しない他の方法:

$dateStart= date("Y-m-d", mktime(0, 0, 0, 10, 1, 2016)); //2016-10-01
$dateEnd = date("Y-m-d", mktime(0, 0, 0, 11, 0, 2016)); //This will return the last day of october, 2016-10-31 :)

だからこのようにそれはそれが31、30または29であるかどうかを計算します

0
Robin Rumeau

私はここで私の日付時刻ヘルパークラスでそれをラップしました https://github.com/normandqq/Date-Time-Helper using $dateLastDay = Model_DTHpr::getLastDayOfTheMonth();

そしてそれは行われます

0
Norman