我試圖從指定的日期獲得一年的日期。
我的代碼如下所示:
$futureDate=date('Y-m-d', strtotime('+one year', $startDate));
它返回錯誤的日期。任何想法爲什麼?
我試圖從指定的日期獲得一年的日期。
我的代碼如下所示:
$futureDate=date('Y-m-d', strtotime('+one year', $startDate));
它返回錯誤的日期。任何想法爲什麼?
要添加一年的今天的日期使用以下命令:
$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"));
添加「+365」而不是「+1年」。謝謝! – Matt 2009-12-15 16:01:28
「* 365天」而不是。 – Matt 2009-12-15 16:03:52
閏年事件不會中斷嗎? – Jeremy1026 2013-01-25 15:46:50
如果你使用PHP 5.3,那是因爲你需要設置默認時區:
date_default_timezone_set()
嘗試這個
$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)), date("d",strtotime($startDate)), date("Y",strtotime($startDate))+1));
strtotime()
正在恢復bool(false)
,因爲它不能解析字符串'+one year'
(它不懂「one」)。然後將false
隱式轉換爲integer
時間戳0
。在您使用其他功能前,驗證strtotime()
的輸出不是bool(false)
是個好主意。
返回值
成功則返回時間戳,FALSE 否則。在PHP 5.1.0之前,這個 函數在失敗時會返回-1。
是的,好點。我的產品代碼會有這樣的功能,但是我試圖讓這個工作起作用,所以把它簡化爲儘可能少的代碼。謝謝! – Matt 2009-12-15 16:02:24
$futureDate=date('Y-m-d', strtotime('+1 year'));
$ futureDate爲一年,從現在開始!
$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate)));
$ futureDate是從$ startDate開始的一年!
至少有人給出了正確的答案,upvote這個人請... – douwe 2013-09-24 14:39:22
剛剛有同樣的問題,但是這是最簡單的解決方案:
<?php (date('Y')+1).date('-m-d'); ?>
偉大的,我用它來得到第二年:'$ next_year =(date('Y')) +1)'。 – Martin 2017-10-14 16:09:58
還有一個更簡單和更復雜的解決方案:
$monthDay = date('m/d');
$year = date('Y')+1;
$oneYearFuture = "".$monthDay."/".$year."";
echo"The date one year in the future is: ".$oneYearFuture."";
在我的情況(我想補充3年至當前日期)解決方案是:
$future_date = date('Y-m-d', strtotime("now + 3 years"));
對Gardenee,Treby和丹尼爾利馬: 2月29日會發生什麼?有時候,二月只有28天:)
世界其他地區的
$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;
闡述你的答案很有幫助。適當的格式也是一個改進。 – 2015-10-29 16:01:18
// 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;
我更喜歡面向對象方法:
$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');
//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'));
希望這個簡單的代碼位可以幫助別人在未來:)
你忘記告訴錯誤。 – BalusC 2009-12-15 03:49:58
弗蘭克法默:你確定嗎?我寧願等待OP的補救/評論。 – BalusC 2009-12-15 04:12:26
昨天晚上我急匆匆地發佈了這個消息,但我忘記了澄清 - 它返回了錯誤的日期。抱歉!謝謝你的幫助。 – Matt 2009-12-15 16:05:18