IT TIP

PHP에서 날짜에 3 개월 추가

itqueen 2020. 10. 25. 13:27
반응형

PHP에서 날짜에 3 개월 추가


2012-03-26$effectiveDate 날짜를 포함 하는 변수가 있습니다 .

이 날짜에 3 개월을 추가하려고하는데 실패했습니다.

내가 시도한 것은 다음과 같습니다.

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));

$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");

내가 도대체 ​​뭘 잘못하고있는 겁니까? 두 코드 모두 작동하지 않았습니다.


다음과 같이 변경하면 예상 형식이 제공됩니다.

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));

"작동하지 않았다"는 것은 형식이 지정된 날짜 대신 타임 스탬프를 제공한다는 것을 의미한다고 가정합니다.

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version

이 대답은 정확히이 질문에 대한 것이 아닙니다. 그러나이 질문은 날짜에서 기간을 추가 / 공제하는 방법을 검색 할 수 있기 때문에 추가 할 것입니다.

$date = new DateTime('now');
$date->modify('+3 month'); // or you can use '-90 day' for deduct
$date = $date->format('Y-m-d h:i:s');
echo $date;

Tchoupi의 대답은 다음과 같이 strtotime ()에 대한 인수를 연결하여 좀 덜 장황하게 만들 수 있습니다.

$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );

(이것은 마법의 구현 세부 사항에 의존하지만, 당신이 정당하게 불신한다면 언제든지 그들을 볼 수 있습니다.)


날짜를 읽을 수있는 값으로 변환해야합니다. strftime () 또는 date ()를 사용할 수 있습니다.

이 시도:

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;

작동합니다. 현지화에 사용할 수 있으므로 strftime을 더 잘 사용하는 것이 좋습니다.


n 번째 일, 월, 년 추가

$n = 2;
for ($i = 0; $i <= $n; $i++){
    $d = strtotime("$i days");
    $x = strtotime("$i month");
    $y = strtotime("$i year");
    echo "Dates : ".$dates = date('d M Y', "+$d days");
    echo "<br>";
    echo "Months : ".$months = date('M Y', "+$x months");
    echo '<br>';
    echo "Years : ".$years = date('Y', "+$y years");
    echo '<br>';
}

다음이 작동해야합니다, 이것을 시도하십시오 :

$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);

다음이 작동하지만 형식을 변경해야 할 수 있습니다.

echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));

PHP Simple Libraries에서 simpleDate 클래스를 사용할 수 있습니다.

include('../code/simpleDate.php');
$date = new simpleDate();
echo $date->set($effectiveDate)->addMonth(3)->get();

여기 에서 라이브러리 자습서를 확인 하십시오 .


다음이 작동합니다.

$d = strtotime("+1 months",strtotime("2015-05-25"));
echo   date("Y-m-d",$d); // This will print **2015-06-25** 

참고 URL : https://stackoverflow.com/questions/9875076/adding-three-months-to-a-date-in-php

반응형