2012-10-09 32 views
4

我是PHP新手,我想在html代碼中添加2個月到特定日期。我該怎麼做?請指導我,如何在HTML代碼中的PHP中添加2個月的日期?

這是我使用的代碼:

private function inputExpiryDate() { 
    $value = $this->expiryDate; 
    $html = ""; 
    $html .= '<label for="expirydate">Expiry Date:</label>'; 
    $html .= '<input type="text" readonly name="expirydate" id="expirydate" value="'.$value.'">'; 
    $html .= '<input type="button" id="expirydatebutton" onclick="getExpiryDate()">' . PHP_EOL; 
    return $html; 
} 

問題是,我將如何添加此

$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 month"); ?? 

任何幫助非常感謝。

+0

當你添加$ value = strtotime(date(「Ymd」,strtotime($ value))。「+1 month」)時會發生什麼;在$ html =「」;?之前? – Osiris

回答

6

你不必分解$date值做相對的日期更改時使用它。這應該只是按預期工作:

$date = date('Y-m-d', strtotime("$date +1 month")); 
+0

謝謝!配合這很簡單。 –

+0

不會負面的,但你的例子不適用於d/m/Y – Matheus

1

你只需要添加date('Y-m-d)這樣date('Y-m-d',strtotime(date("Y-m-d", strtotime($date)) . " +1 month"))

3

添加行

echo date("Y-m-d", $date); 

由於第一句話將返回Unix時間戳,所以你需要再次將其轉換爲可讀格式

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");