티스토리 뷰
오늘 날짜에 1 년을 추가하려면 다음을 사용하십시오.
$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"));
-------------------
$futureDate=date('Y-m-d', strtotime('+1 year'));
$ futureDate는 지금부터 1 년 후입니다!
$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate)) );
$ futureDate는 $ startDate로부터 1 년입니다!-------------------
시험:
$futureDate=date('Y-m-d',strtotime('+1 year',$startDate));
-------------------
// 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;
-------------------
같은 문제가 있었지만 이것이 가장 간단한 해결책이었습니다.
<?php (date('Y')+1).date('-m-d'); ?>
-------------------
//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'));
이 간단한 코드가 미래의 누군가에게 도움이되기를 바랍니다. :)-------------------
OO 접근 방식을 선호합니다.
$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');
-------------------
PHP 5.3을 사용하는 경우 기본 시간대를 설정해야하기 때문입니다.
date_default_timezone_set()
-------------------
strtotime()
bool(false)
문자열을 구문 분석 할 수 없기 때문에를 반환 합니다
'+one year'
( "하나"를 이해하지 못함).
false
그런 다음 암시 적으로
integer
타임 스탬프로 캐스팅됩니다
0
. 다른 기능에 밀어 넣기 전에
strtotime()
의 출력이 아닌지 확인하는 것이 좋습니다
bool(false)
.
문서에서 :
반환 값성공하면 타임 스탬프를 반환하고 그렇지 않으면 FALSE를 반환합니다. PHP 5.1.0 이전에는이 함수가 실패시 -1을 반환했습니다.
-------------------
이 시도
$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)), date("d",strtotime($startDate)), date("Y",strtotime($startDate))+1));
-------------------
더 간단하고 덜 정교한 솔루션도 있습니다.
$monthDay = date('m/d');
$year = date('Y')+1;
$oneYearFuture = "".$monthDay."/".$year."";
echo"The date one year in the future is: ".$oneYearFuture."";
-------------------
내 솔루션은 다음과 같습니다.
date('Y-m-d', time()-60*60*24*365);
정의를 사용하여 더 "가독성"을 높일 수 있습니다.
define('ONE_SECOND', 1);
define('ONE_MINUTE', 60 * ONE_SECOND);
define('ONE_HOUR', 60 * ONE_MINUTE);
define('ONE_DAY', 24 * ONE_HOUR);
define('ONE_YEAR', 365 * ONE_DAY);
date('Y-m-d', time()-ONE_YEAR);
-------------------
제 경우에는 (현재 날짜에 3 년을 추가하고 싶습니다) 해결책은 다음과 같습니다.
$future_date = date('Y-m-d', strtotime("now + 3 years"));
Gardenee, Treby 및 Daniel Lima에게 : 2 월 29 일에는 어떤 일이 일어날까요? 때때로 2 월은 28 일 밖에 없습니다 :)
출처
https://stackoverflow.com/questions/1905048