77

私はこのPHPコードを持っています:

$end=date('Y-m-d');

私はそれを使用して現在の日付を取得します。次のような 5 年後の日付が必要です。

$end=date('(Y + 5)-m-d');

これどうやってするの?

4

12 に答える 12

169

試してみてください:

$end = date('Y-m-d', strtotime('+5 years'));
于 2013-08-16T09:02:33.470 に答える
28

この投稿に基づいて 日付を変更
する strtotime() は非常に強力で、相対式でも簡単に日付を変更/変換できます

:

    $dateString = '2011-05-01 09:22:34';
    $t = strtotime($dateString);
    $t2 = strtotime('-3 days', $t);
    echo date('r', $t2) . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100

日付時刻

    $dateString = '2011-05-01 09:22:34';
    $dt = new DateTime($dateString);
    $dt->modify('-3 days');
    echo $dt->format('r') . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100

strtotime() で投げることができるものは、非常に驚​​くべきものであり、非常に人間が読めるものです。来週の火曜日を探しているこの例を見てください。

手続き型

    $t = strtotime("Tuesday next week");
    echo date('r', $t) . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100

日付時刻

    $dt = new DateTime("Tuesday next week");
    echo $dt->format('r') . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100

上記の例は、現在の時刻を基準にして返されていることに注意してください。strtotime() と DateTime コンストラクターが取る時刻形式の完全なリストは、PHP でサポートされている日付と時刻の形式のページにリストされています。

あなたのケースに適した別の例は次のとおりです: この投稿に基づいて

    <?php
    //How to get the day 3 days from now:
    $today = date("j");
    $thisMonth = date("n");
    $thisYear = date("Y");
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear)); 

    //1 week from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear));

    //4 months from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear)); 

    //3 years, 2 months and 35 days from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3));
    ?>
于 2013-08-16T09:20:43.143 に答える
2

カーボンの使用:

$dt = Carbon::now();
echo $dt->addYears(5); 
于 2013-08-16T09:02:14.247 に答える