2009-12-15 28 views
54

我試圖從指定的日期獲得一年的日期。

我的代碼如下所示:

$futureDate=date('Y-m-d', strtotime('+one year', $startDate)); 

它返回錯誤的日期。任何想法爲什麼?

+8

你忘記告訴錯誤。 – BalusC 2009-12-15 03:49:58

+0

弗蘭克法默:你確定嗎?我寧願等待OP的補救/評論。 – BalusC 2009-12-15 04:12:26

+0

昨天晚上我急匆匆地發佈了這個消息,但我忘記了澄清 - 它返回了錯誤的日期。抱歉!謝謝你的幫助。 – Matt 2009-12-15 16:05:18

回答

61

要添加一年的今天的日期使用以下命令:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day")); 

對於其他的例子,你必須初始化$ STARTINGDATE與時間戳值 例如:

$StartingDate = mktime(); // todays date as a timestamp 

試試這個

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 365 day")); 

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 1 year")); 
+0

添加「+365」而不是「+1年」。謝謝! – Matt 2009-12-15 16:01:28

+0

「* 365天」而不是。 – Matt 2009-12-15 16:03:52

+9

閏年事件不會中斷嗎? – Jeremy1026 2013-01-25 15:46:50

6

嘗試:$futureDate=date('Y-m-d',strtotime('+1 year',$startDate));

+0

雖然這確實會返回正確的答案,但另一個實際上並不會返回錯誤...只是錯誤的日期。 – SeanJA 2009-12-15 03:56:26

+1

strtotime不會拋出錯誤。它在錯誤的情況下返回false。 – 2009-12-15 04:01:13

+0

如果默認的時區未設置,PHP會發出警告......雖然顯然這不是他的意思 – SeanJA 2009-12-18 12:32:12

2

如果你使用PHP 5.3,那是因爲你需要設置默認時區:

date_default_timezone_set() 
1

嘗試這個

$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)), date("d",strtotime($startDate)), date("Y",strtotime($startDate))+1)); 
2

strtotime()正在恢復bool(false) ,因爲它不能解析字符串'+one year'(它不懂「one」)。然後將false隱式轉換爲integer時間戳0。在您使用其他功能前,驗證strtotime()的輸出不是bool(false)是個好主意。

From the docs:

返回值

成功則返回時間戳,FALSE 否則。在PHP 5.1.0之前,這個 函數在失敗時會返回-1。

+0

是的,好點。我的產品代碼會有這樣的功能,但是我試圖讓這個工作起作用,所以把它簡化爲儘可能少的代碼。謝謝! – Matt 2009-12-15 16:02:24

152
$futureDate=date('Y-m-d', strtotime('+1 year')); 

$ futureDate爲一年,從現在開始!

$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate))); 

$ futureDate是從$ startDate開始的一年!

+3

至少有人給出了正確的答案,upvote這個人請... – douwe 2013-09-24 14:39:22

4

剛剛有同樣的問題,但是這是最簡單的解決方案:

<?php (date('Y')+1).date('-m-d'); ?> 
+0

偉大的,我用它來得到第二年:'$ next_year =(date('Y')) +1)'。 – Martin 2017-10-14 16:09:58

0

還有一個更簡單和更復雜的解決方案:

$monthDay = date('m/d'); 
$year = date('Y')+1; 
$oneYearFuture = "".$monthDay."/".$year.""; 
echo"The date one year in the future is: ".$oneYearFuture.""; 
-2

在我的情況(我想補充3年至當前日期)解決方案是:

$future_date = date('Y-m-d', strtotime("now + 3 years")); 

對Gardenee,Treby和丹尼爾利馬: 2月29日會發生什麼?有時候,二月只有28天:)

-2

世界其他地區的

$now = time(); 
$future = date("d-m-Y", strtotime(date("d-m-Y", $now) . " + 1 year")); 
echo $future; 

美國

$now = time(); 
$future = date("Y-m-d", strtotime(date("Y-m-d", $now) . " + 1 year")); 
echo $future; 
+1

闡述你的答案很有幫助。適當的格式也是一個改進。 – 2015-10-29 16:01:18

5
// Declare a variable for this year 
$this_year = date("Y"); 
// Add 1 to the variable 
$next_year = $this_year + 1; 
$year_after = $this_year + 2; 

// Check your code 
    echo "This year is "; 
    echo $this_year; 
    echo "<br />"; 
    echo "Next year is "; 
    echo $next_year; 
    echo "<br />"; 
    echo "The year after that is "; 
    echo $year_after; 
1

我更喜歡面向對象方法:

$date = new \DateTimeImmutable('today'); //'today' gives midnight, leave blank for current time. 
$futureDate = $date->add(\DateInterval::createFromDateString('+1 Year')) 

使用DateTimeImmutable否則你會修改原來的日期呢! 更多DateTimeImmutable:http://php.net/manual/en/class.datetimeimmutable.php


如果你只是從今天的日期要那麼你可以隨時做:

new \DateTimeImmutable('-1 Month'); 
0
//1 year from today's date 
echo date('d-m-Y', strtotime('+1 year')); 

//1 year from from specific date 
echo date('22-09-Y', strtotime('+1 year')); 

希望這個簡單的代碼位可以幫助別人在未來:)