오늘 날짜에서 다음 달 날짜를 가져와서 데이터베이스에 삽입하려면 어떻게 해야 합니까?
내 DB에는 두 개의 열이 있습니다.start_date그리고.end_date둘 다DATE제 코드는 다음과 같이 날짜를 업데이트하고 있습니다.
$today_date = date("Y-m-d");
$end_date = date("Y-m-d"); // date +1 month ??
$sql1 = "UPDATE `users` SET `start_date` = '".$today_date."', `end_date` = '".$end_date."' WHERE `users`.`id` ='".$id."' LIMIT 1 ;";
만드는 가장 좋은 방법은 무엇입니까?$end_date와 동등한.$start_date한 달?예를 들어, 2000-10-01은 2000-11-01이 됩니다.
PHP의 strtime() 함수를 사용할 수 있습니다.
// One month from today
$date = date('Y-m-d', strtotime('+1 month'));
// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
참고로+1 month항상 직관적으로 계산되는 것은 아닙니다.항상 현재 달에 존재하는 일 수를 추가하는 것으로 나타납니다.
Current Date | +1 month
-----------------------------------------------------
2015-01-01 | 2015-02-01 (+31 days)
2015-01-15 | 2015-02-15 (+31 days)
2015-01-30 | 2015-03-02 (+31 days, skips Feb)
2015-01-31 | 2015-03-03 (+31 days, skips Feb)
2015-02-15 | 2015-03-15 (+28 days)
2015-03-31 | 2015-05-01 (+31 days, skips April)
2015-12-31 | 2016-01-31 (+31 days)
사용할 수 있는 기타 날짜/시간 간격:
$date = date('Y-m-d'); // Initial date string to use in calculation
$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+2 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 month', strtotime($date)));
$date = date('Y-m-d', strtotime('+30 days', strtotime($date)));
승인된 답변은 정확히 31일 후에 원하는 경우에만 작동합니다.그것은 당신이 6월에 없을 것으로 예상되는 "2013-05-31" 날짜를 사용하고 있다면 제가 원했던 것이 아니라는 것을 의미합니다.
다음 달을 원하신다면 현재의 연도와 월을 사용하되 1일을 계속 사용하는 것을 제안합니다.
$date = date("Y-m-01");
$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;
이렇게 하면 한 달을 건너뛰지 않고 다음 달의 월과 연도를 얻을 수 있습니다.
이를 위해 실제로 PHP 기능이 필요하지 않습니다.SQL에서 이미 간단한 날짜 조작을 직접 수행할 수 있습니다. 예:
$sql1 = "
UPDATE `users` SET
`start_date` = CURDATE(),
`end_date` = DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
WHERE `users`.`id` = '" . $id . "';
";
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime 를 참조하십시오.
다음 달의 특정 날짜를 원하는 경우 다음을 수행할 수 있습니다.
// Calculate the timestamp
$expire = strtotime('first day of +1 month');
// Format the timestamp as a string
echo date('m/d/Y', $expire);
참고로 이것은 실제로 더 안정적으로 작동합니다.+1 month헷갈릴 수 있습니다.예를 들면...
Current Day | first day of +1 month | +1 month
---------------------------------------------------------------------------
2015-01-01 | 2015-02-01 | 2015-02-01
2015-01-30 | 2015-02-01 | 2015-03-02 (skips Feb)
2015-01-31 | 2015-02-01 | 2015-03-03 (skips Feb)
2015-03-31 | 2015-04-01 | 2015-05-01 (skips April)
2015-12-31 | 2016-01-01 | 2016-01-31
여기에 제 솔루션을 추가합니다. 이것은 구글 검색에 나오는 스레드이기 때문입니다.이것은 다음 달 날짜를 얻고, 건너뛰기를 고치고, 다음 달 안에 다음 날짜를 유지하기 위한 것입니다.
PHP는 현재 월 총 일 수를 현재 날짜에 추가합니다.+1 month예를들면.
그래서 신청하는 것.+1 month로.30-01-2016돌아올 것입니다02-03-2016(추가)31일)
저 같은 경우에는, 제가 필요한 것은28-02-2016다음 달 안으로 유지하기 위해이러한 경우 아래 솔루션을 사용할 수 있습니다.
이 코드는 지정된 날짜의 날짜가 다음 달의 총 날짜보다 큰지 여부를 식별합니다.그렇다면 날짜를 스마트하게 빼고 월 범위 내에서 날짜를 반환합니다.
반환 값은 타임스탬프 형식입니다.
function getExactDateAfterMonths($timestamp, $months){
$day_current_date = date('d', $timestamp);
$first_date_of_current_month = date('01-m-Y', $timestamp);
// 't' gives last day of month, which is equal to no of days
$days_in_next_month = date('t', strtotime("+".$months." months", strtotime($first_date_of_current_month)));
$days_to_substract = 0;
if($day_current_date > $days_in_next_month)
$days_to_substract = $day_current_date - $days_in_next_month;
$php_date_after_months = strtotime("+".$months." months", $timestamp);
$exact_date_after_months = strtotime("-".$days_to_substract." days", $php_date_after_months);
return $exact_date_after_months;
}
getExactDateAfterMonths(strtotime('30-01-2016'), 1);
// $php_date_after_months => 02-03-2016
// $exact_date_after_months => 28-02-2016
가글러의 예와 같이 str to time()을 사용할 수 있으며, 이 경우에 매우 좋습니다.
더 복잡한 제어가 필요한 경우 mktime()을 사용합니다.
$end_date = mktime(date("H"), date("i"), date("s"), date("n") + 1, date("j"), date("Y"));
date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))
date_trunc('month', now()) + interval '1 month'
date_trunc('month', now())합니다.+ interval '1 month'날짜에 한 달을 더하다
이 함수는 올바른 월 수를 양수 또는 음수로 반환합니다.다음 설명 섹션에서 확인할 수 있습니다.
function addMonthsToTime($numMonths = 1, $timeStamp = null){
$timeStamp === null and $timeStamp = time();//Default to the present
$newMonthNumDays = date('d',strtotime('last day of '.$numMonths.' months', $timeStamp));//Number of days in the new month
$currentDayOfMonth = date('d',$timeStamp);
if($currentDayOfMonth > $newMonthNumDays){
$newTimeStamp = strtotime('-'.($currentDayOfMonth - $newMonthNumDays).' days '.$numMonths.' months', $timeStamp);
} else {
$newTimeStamp = strtotime($numMonths.' months', $timeStamp);
}
return $newTimeStamp;
}
01-2014년 2월
$date = mktime( 0, 0, 0, 2, 1, 2014 );
echo strftime( '%d %B %Y', strtotime( '+1 month', $date ) );
나는 이것이 Kouton의 대답과 비슷하다고 생각하지만, 이것은 타임스탬프를 가져와서 다음 달 어딘가에 타임스탬프를 반환합니다.여기서 날짜("m", $nextMonthDateN)를 사용할 수 있습니다.
function nextMonthTimeStamp($curDateN){
$nextMonthDateN = $curDateN;
while( date("m", $nextMonthDateN) == date("m", $curDateN) ){
$nextMonthDateN += 60*60*24*27;
}
return $nextMonthDateN; //or return date("m", $nextMonthDateN);
}
알아요 - 좀 늦은 것 같아요.하지만 저는 같은 문제를 해결하고 있었습니다.고객이 한 달간 서비스를 구매하면 한 달 뒤 종료될 것으로 예상하고 있습니다.해결 방법은 다음과 같습니다.
$now = time();
$day = date('j',$now);
$year = date('o',$now);
$month = date('n',$now);
$hour = date('G');
$minute = date('i');
$month += $count;
if ($month > 12) {
$month -= 12;
$year++;
}
$work = strtotime($year . "-" . $month . "-01");
$avail = date('t',$work);
if ($day > $avail)
$day = $avail;
$stamp = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute);
지금부터 n*개의 정확한 날짜를 계산합니다(여기서 <= 12).2019년 3월 31일에 시작하여 11개월 동안 서비스가 진행되면 2020년 2월 29일에 종료됩니다.한 달만 실행하면 종료일은 2019년 4월 30일입니다.
$nextm = date('m', strtotime('+1개월', strtotime(날짜('Y-m-01'));
이를 수행하는 한 가지 방법은 - 먼저 현재 달의 마지막 날짜를 얻은 후 다음 달의 첫 번째 날짜를 얻기 위해 하루를 추가하는 것입니다.이렇게 하면 '+1개월'을 사용하는 동안 의도하지 않게 몇 달을 건너뛰는 것을 방지할 수 있습니다.
$today_date = date("Y-m-d");
$last_date_of_month=date('Y-m-t',strtotime($today_date);//get last date of current month
$last_day_of_month_proper =strtotime($last_day_of_month);
$new_date=date('Y-m-d', strtotime('+1 day',$last_day_of_month_proper));
echo $new_date;
언급URL : https://stackoverflow.com/questions/4318991/how-do-i-get-next-month-date-from-todays-date-and-insert-it-in-my-database
'programing' 카테고리의 다른 글
| TypeVar와 NewType의 차이점은 무엇입니까? (0) | 2023.08.28 |
|---|---|
| Angular [사용 안 함]="MyBoolean"이 작동하지 않음 (0) | 2023.08.28 |
| ExpressJS 인스턴스를 프로그래밍 방식으로 종료하려면 어떻게 해야 합니까? (0) | 2023.08.28 |
| 보기에 힌트를 사용하시겠습니까? (0) | 2023.08.28 |
| 명령줄에서 사용자 로그아웃 (0) | 2023.08.28 |