简体   繁体   English

如何从今天的日期获取下个月的日期并将其插入到我的数据库中?

[英]How do I get next month date from today's date and insert it in my database?

I have two columns in my db: start_date and end_date , which are both DATE types.我的数据库中有两列: start_dateend_date ,它们都是DATE类型。 My code is updating the dates as follows:我的代码更新日期如下:

$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 ;";

What is the best way to make $end_date equal to $start_date + one month?使$end_date等于$start_date + 一个月的最佳方法是什么? For example, 2000- 10 -01 would become 2000- 11 -01.例如,2000- 10 -01 将变成 2000- 11 -01。

You can use PHP's strtotime() function:您可以使用 PHP 的strtotime()函数:

// 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')));

Just note that +1 month is not always calculated intuitively.请注意, +1 month并不总是直观地计算出来的。 It appears to always add the number of days that exist in the current 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)

Some other date/time intervals that you can use:您可以使用的其他一些日期/时间间隔:

$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)));

The accepted answer works only if you want exactly 31 days later.接受的答案仅在您想要 31 天后才有效。 That means if you are using the date "2013-05-31" that you expect to not be in June which is not what I wanted.这意味着如果您使用的日期“2013-05-31”,您预计不会在 6 月,这不是我想要的。

If you want to have the next month, I suggest you to use the current year and month but keep using the 1st.如果你想要下个月,我建议你使用当前的年份和月份,但继续使用第一个。

$date = date("Y-m-01");
$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;

This way, you will be able to get the month and year of the next month without having a month skipped.这样,您将能够获得下个月的月份和年份,而不会跳过一个月。

You do not actually need PHP functions to achieve this.您实际上并不需要 PHP 函数来实现这一点。 You can already do simple date manipulations directly in SQL, for example:您已经可以直接在 SQL 中进行简单的日期操作,例如:

$sql1 = "
    UPDATE `users` SET 
    `start_date` = CURDATE(), 
    `end_date` = DATE_ADD(CURDATE(), INTERVAL 1 MONTH)  
    WHERE `users`.`id` = '" . $id . "';
";

Refer to http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime参考http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime

If you want a specific date in next month, you can do this:如果您想要下个月的特定日期,您可以这样做:

// Calculate the timestamp
$expire = strtotime('first day of +1 month');

// Format the timestamp as a string
echo date('m/d/Y', $expire);

Note that this actually works more reliably where +1 month can be confusing.请注意,这实际上在+1 month可能令人困惑的情况下更可靠。 For example...例如...

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

Be careful, because sometimes strtotime("+1 months") can skip month numbers.小心,因为有时strtotime("+1 months")可以跳过月份数字。

Example:例子:

$today = date("Y-m-d"); // 2012-01-30
$next_month = date("Y-m-d", strtotime("$today +1 month"));

If today is January, next month should be February which has 28 or 29 days, but PHP will return March as next month, not February.如果今天是一月,那么下个月应该是二月,有 28 或 29 天,但 PHP 会将三月返回为下个月,而不是二月。

date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))

You can use strtotime() as in Gazler's example, which is great for this case.您可以在 Gazler 的示例中使用strtotime() ,这对这种情况非常有用。

If you need more complicated control use mktime() .如果您需要更复杂的控制,请使用mktime()

$end_date = mktime(date("H"), date("i"), date("s"), date("n") + 1, date("j"), date("Y"));

Adding my solution here, as this is the thread that comes in google search.在这里添加我的解决方案,因为这是谷歌搜索中的线程。 This is to get the next date of month, fixing any skips, keeping the next date within next month.这是为了获取下一个月的日期,修复任何跳过,将下一个日期保持在下个月内。

PHP adds current months total number of days to current date, if you do +1 month for example. PHP 将当前月份的总天数添加到当前日期,例如,如果您执行+1 month

So applying +1 month to 30-01-2016 will return 02-03-2016 .因此,将+1 month应用到30-01-2016将返回02-03-2016 (Adding 31 days) (增加31天)

For my case, I needed to get 28-02-2016 , so as to keep it within next month.就我而言,我需要获得28-02-2016 ,以便将其保留在下个月内。 In such cases you can use the solution below.在这种情况下,您可以使用以下解决方案。

This code will identify if the given date's day is greater than next month's total days.此代码将识别给定日期的天数是否大于下个月的总天数。 If so It will subtract the days smartly and return the date within the month range.如果是这样,它将巧妙地减去天数并返回月份范围内的日期。

Do note the return value is in timestamp format.请注意,返回值采用时间戳格式。

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

This function returns any correct number of months positively or negatively.此函数正负返回任何正确的月数。 Found in the comment section here :这里评论部分找到

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-Feb-2014 2014 年 2 月 1 日

$date = mktime( 0, 0, 0, 2, 1, 2014 );

echo strftime( '%d %B %Y', strtotime( '+1 month', $date ) );

I think this is similar to kouton's answer, but this one just takes in a timeStamp and returns a timestamp SOMEWHERE in the next month.我认为这类似于 kouton 的答案,但这个答案只是接受一个时间戳并在下个月的某个地方返回一个时间戳。 You could use date("m", $nextMonthDateN) from there.您可以从那里使用 date("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);
}

I know - sort of late.我知道 - 有点晚了。 But I was working at the same problem.但我正在解决同样的问题。 If a client buys a month of service, he/she expects to end it a month later.如果客户购买了一个月的服务,他/她希望在一个月后结束服务。 Here's how I solved it:这是我解决它的方法:

    $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);

This will calculate the exact day n*count months from now (where count <= 12).这将计算从现在开始的确切日期 n*count 个月(其中 count <= 12)。 If the service started March 31, 2019 and runs for 11 months, it will end on Feb 29, 2020. If it runs for just one month, the end date is Apr 30, 2019.如果该服务于 2019 年 3 月 31 日开始并运行 11 个月,则将于 2020 年 2 月 29 日结束。如果仅运行 1 个月,则结束日期为 2019 年 4 月 30 日。

$nextm = date('m', strtotime('+1 月', strtotime(date('Ym-01'))));

one way of doing this is- first getting last date of current month and then adding 1 day to it, to get next month's first date.这样做的一种方法是 - 首先获取当月的最后日期,然后添加 1 天,以获得下个月的第一个日期。 This will prevent us from unintentional skipping of months which occurs while using '+1 month'.这将防止我们在使用“+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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM