지난달 날짜를 php로 가져오는 중
지난달 날짜를 알고 싶어요.이렇게 적어놨어요.
$prevmonth = date('M Y');
그래서 지금의 달/년도를 알 수 있습니다. 써야 할지 모르겠어요.strtotime
,mktime
임스스 ?? ?? ????사이트의 모든 타임스탬프에 대해 날짜가 지난달로 설정되지 않도록 리셋하기 위해 나중에 무언가를 추가해야 합니까?RTM을 시도하고 있지만, 이것을 이해하는 것은 어렵습니다.
지난달 날짜를 얻는 것은 간단하다.
echo date("Y-n-j", strtotime("first day of previous month"));
echo date("Y-n-j", strtotime("last day of previous month"));
11월 3일에
2014-10-1
2014-10-31
echo strtotime("-1 month");
그것은 지난달의 타임스탬프를 정확하게 출력할 것이다.그 후 아무것도 리셋할 필요가 없습니다.그 후 영어 형식으로 하려면 date()를 사용하여 타임스탬프를 포맷할 수 있습니다.
echo date("Y-m-d H:i:s",strtotime("-1 month"));
$prevmonth = date('M Y', strtotime("last month"));
오답은 다음과 같습니다.
$lastMonth = date('M Y', strtotime("-1 month"));
$lastDate = date('Y-m', strtotime('last month'));
그 이유는 현재 달이 30일 이상인데 전 달이 29일이고 $last Month가 현재 달과 같기 때문입니다.
예.
If $currentMonth = '30/03/2016';
echo $lastMonth = date('m-Y', strtotime("-1 month")); => 03-2016
echo $lastDate = date('Y-m', strtotime('last month')); => 2016-03
정답은 다음과 같습니다.
echo date("m-Y", strtotime("first day of previous month")); => 02-2016
echo sprintf("%02d",date("m")-1) . date("-Y"); => 02-2016
echo date("m-Y",mktime(0,0,0,date("m")-1,1,date("Y"))); => 02-2016
이전 달이 현재보다 짧으면 이번 달은 잘못된 것으로 판명되었습니다.
echo date("Y-m-d H:i:s",strtotime("-1 month"));
3월 30일에 시도하면 2012-02가 아닌 2012-03-01이 나옵니다.
더 나은 해결책을 찾는 중...
만약 당신이 바로 전월을 얻고 싶다면, 당신은 다음과 같이 사용할 수 있습니다.
$prevmonth = date('M Y', strtotime('-1 months'));
만약 당신이 전월과 같은 날을 얻고 싶다면, 당신은 다음과 같이 사용할 수 있습니다.
$prevmonth = date('M Y d', strtotime('-1 months'));
만약 당신이 전월의 마지막 날짜를 얻고 싶다면, 당신은 다음과 같이 사용할 수 있습니다.
$prevmonth = date('M Y t', strtotime('-1 months'));
만약 당신이 전월의 첫 번째 날짜를 얻고 싶다면, 당신은 다음과 같이 사용할 수 있습니다.
$prevmonth = date('M Y 1', strtotime('-1 months'));
echo date('Y',strtotime("-1 year")); //last year<br>
echo date('d',strtotime("-1 day")); //last day<br>
echo date('m',strtotime("-1 month")); //last month<br>
public function getLastMonth() {
$now = new DateTime();
$lastMonth = $now->sub(new DateInterval('P1M'));
return $lastMonth->format('Ym');
}
다음 짧은 코드를 사용하여 특정 날짜에 대한 전월을 가져옵니다.
$tgl = '25 january 2012';
$prevmonth = date("M Y",mktime(0,0,0,date("m", strtotime($tgl))-1,1,date("Y", strtotime($tgl))));
echo $prevmonth;
결과는 2011년 12월입니다.전월보다 날이 짧은 달에 작업합니다.
$lastMonth = date('M Y', strtotime("-1 month"));
var_dump($lastMonth);
$lastMonth = date('M Y', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
var_dump($lastMonth);
를 사용할 수 있습니다.이러한 상황에서는 매우 편리합니다.
$timestamp = strtotime('-1 month');
var_dump(date('Y-m', $timestamp));
다음 정보를 얻을 수 있습니다.
string '2009-11' (length=7)
$time = mktime(0, 0, 0, date("m"),date("d")-date("t"), date("Y"));
$lastMonth = date("d-m-Y", $time);
또는
$lastMonth = date("m-Y", mktime() - 31*3600*24);
는 30.03.2012에서 동작합니다.
아, 내가 알아냈으니, 내가 했던 것과 같은 문제가 아니라면 무시해 주세요.
$prevmonth = date("M Y",mktime(0,0,0,date("m")-1,1,date("Y")));
이 질문은 꽤 오래되었지만 어쨌든 간다.전월만 가져오려고 하고 날짜가 중요하지 않은 경우 다음을 사용할 수 있습니다.
$date = '2014-01-03';
$dateTime = new DateTime($date);
$lastMonth = $dateTime->modify('-' . $dateTime->format('d') . ' days')->format('F Y');
echo $lastMonth; // 'December 2013'
제가 찾은 최고의 솔루션은 다음과 같습니다.
function subtracMonth($currentMonth, $monthsToSubtract){
$finalMonth = $currentMonth;
for($i=0;$i<$monthsToSubtract;$i++) {
$finalMonth--;
if ($finalMonth=='0'){
$finalMonth = '12';
}
}
return $finalMonth;
}
그래서 만약 우리가 3(3월)에 있고 5개월을 빼려면
subtractMonth(3,5);
그럼 10월이면 되겠네요.연도도 원하는 경우 다음을 수행할 수 있습니다.
function subtracMonth($currentMonth, $monthsToSubtract){
$finalMonth = $currentMonth;
$totalYearsToSubtract = 0;
for($i=0;$i<$monthsToSubtract;$i++) {
$finalMonth--;
if ($finalMonth=='0'){
$finalMonth = '12';
$totalYearsToSubtract++;
}
}
//Get $currentYear
//Calculate $finalYear = $currentYear - $totalYearsToSubtract
//Put resulting $finalMonth and $finalYear into an object as attributes
//Return the object
}
효과가 있습니다.
오늘은 2012년 3월 31일
echo date("Y-m-d", strtotime(date('m', mktime() - 31*3600*24).'/01/'.date('Y').' 00:00:00')); // 2012-02-01
echo date("Y-m-d", mktime() - 31*3600*24); // 2012-02-29
전월의 첫 번째 날짜를 얻고 싶다면 다음과 같이 사용할 수 있습니다. $prevmonth = date('M Y 1', strtotime('-1 months'));
뭐? 첫 데이트는 항상 1:D일거야.
그냥 지난달로 넘어가세요.
예:
오늘은 2020-09-02
코드:
echo date('Y-m-d', strtotime(date('Y-m-d')." -1 month"));
결과:
2020-08-02
언급URL : https://stackoverflow.com/questions/1889758/getting-last-months-date-in-php
'itsource' 카테고리의 다른 글
각 유형에서 가장 큰 행 선택 방법 (0) | 2022.09.12 |
---|---|
원칙 2에서 엔티티를 다른 행으로 다시 저장하는 방법 (0) | 2022.09.12 |
SQL select return 값을 바꾸는 방법 (0) | 2022.09.12 |
스캐너 클래스에서 next() 메서드와 nextLine() 메서드의 차이점은 무엇입니까? (0) | 2022.09.12 |
C#에 null margeing 연산자(--)와 동등한 Java가 있습니까? (0) | 2022.09.12 |