我正在尝试从我指定的日期开始一年的日期.
我的代码看起来像这样:
$futureDate=date('Y-m-d', strtotime('+one year', $startDate));
Run Code Online (Sandbox Code Playgroud)
它回复了错误的日期.有什么想法吗?
Mis*_*sho 194
$futureDate=date('Y-m-d', strtotime('+1 year'));
Run Code Online (Sandbox Code Playgroud)
$ futureDate是一年后的!
$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate)) );
Run Code Online (Sandbox Code Playgroud)
$ futureDate是$ startDate的一年!
Nid*_*aby 84
要在今天的日期添加一年,请使用以下内容:
$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
Run Code Online (Sandbox Code Playgroud)
对于其他示例,您必须使用时间戳值初始化$ StartingDate,例如:
$StartingDate = mktime(); // todays date as a timestamp
Run Code Online (Sandbox Code Playgroud)
试试这个
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 365 day"));
Run Code Online (Sandbox Code Playgroud)
要么
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 1 year"));
Run Code Online (Sandbox Code Playgroud)
//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'));
Run Code Online (Sandbox Code Playgroud)
希望这段更简单的代码可以帮助将来的某个人:)
// 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;
Run Code Online (Sandbox Code Playgroud)
小智 5
只是有同样的问题,但这是最简单的解决方案:
<?php (date('Y')+1).date('-m-d'); ?>
Run Code Online (Sandbox Code Playgroud)
我更喜欢面向对象的方法:
$date = new \DateTimeImmutable('today'); //'today' gives midnight, leave blank for current time.
$futureDate = $date->add(\DateInterval::createFromDateString('+1 Year'))
Run Code Online (Sandbox Code Playgroud)
使用DateTimeImmutable
,否则你将改变原来的日期呢!更多关于 DateTimeImmutable:http : //php.net/manual/en/class.datetimeimmutable.php
如果您只想从今天的日期开始,那么您可以随时执行以下操作:
new \DateTimeImmutable('-1 Month');
Run Code Online (Sandbox Code Playgroud)