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 일이고 $ lastMonth보다 적은 금액이 이번 달과 동일하기 때문입니다.
예 :
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
지난달 만 받고 싶다면 다음과 같이 사용할 수 있습니다.
$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-m-d H:i:s",strtotime("-1 month"));
3 월 30 일에 시도하면 2012-02 대신 2012-03-01을 받게됩니다.
더 나은 솔루션을 찾는 중 ...
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');
}
$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);
을 사용할 수 있습니다 strtotime
. 이는 이러한 상황에서 유용 합니다.
$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에 작동
Oh I figured this out, please ignore unless you have the same problem i did in which case:
$prevmonth = date("M Y",mktime(0,0,0,date("m")-1,1,date("Y")));
Use this short code to get previous month for any given date:
$tgl = '25 january 2012';
$prevmonth = date("M Y",mktime(0,0,0,date("m", strtotime($tgl))-1,1,date("Y", strtotime($tgl))));
echo $prevmonth;
The result is December 2011. Works on a month with shorter day with previous month.
It works for me:
Today is: 31/03/2012
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
If you want to get first date of previous month , Then you can use as like following ... $prevmonth = date('M Y 1', strtotime('-1 months'));
what? first date will always be 1 :D
This question is quite old but here goes anyway. If you're trying to get just the previous month and the day does not matter you can use this:
$date = '2014-01-03';
$dateTime = new DateTime($date);
$lastMonth = $dateTime->modify('-' . $dateTime->format('d') . ' days')->format('F Y');
echo $lastMonth; // 'December 2013'
참고URL : https://stackoverflow.com/questions/1889758/getting-last-months-date-in-php
'development' 카테고리의 다른 글
ActiveRecord 콜백을 건너 뛰는 방법은 무엇입니까? (0) | 2020.10.15 |
---|---|
PHP를 사용하여 CSRF 토큰을 올바르게 추가하는 방법 (0) | 2020.10.15 |
PHP, 날짜에서 내일 날짜 가져 오기 (0) | 2020.10.15 |
파일 이름에서 디렉토리 이름 얻기 (0) | 2020.10.15 |
TextView 텍스트에 이미지를 추가하는 방법은 무엇입니까? (0) | 2020.10.15 |