php 如何从今天的日期获取下个月的日期并将其插入到我的数据库中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4318991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get next month date from today's date and insert it in my database?
提问by Saleh
I have two columns in my db: start_date
and end_date
, which are both DATE
types. My code is updating the dates as follows:
我的 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 ;";
What is the best way to make $end_date
equal to $start_date
+ one month? For example, 2000-10-01 would become 2000-11-01.
使$end_date
等于$start_date
+ 一个月的最佳方法是什么?例如,2000- 10-01 将变成 2000- 11-01。
回答by Gazler
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. It appears to always add the number of days that exist in the current month.
请注意,这+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)
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)));
回答by Patrick Desjardins
The accepted answer works only if you want exactly 31 days later. 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.
接受的答案仅在您想要 31 天后才有效。这意味着如果您使用的日期“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.
这样,您将能够获得下个月的月份和年份,而不会跳过一个月。
回答by Anton S
You do not actually need PHP functions to achieve this. You can already do simple date manipulations directly in SQL, for example:
您实际上并不需要 PHP 函数来实现这一点。您已经可以直接在 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
回答by Gil Prata
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. For example...
请注意,这实际上在+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
回答by Lauris Kuznecovs
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 会将三月返回为下个月,而不是二月。
回答by Orbling
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"));
回答by leon
date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))
回答by Shah Abaz Khan
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
. (Adding 31
days)
因此应用+1 month
到30-01-2016
将返回02-03-2016
。(添加31
天数)
For my case, I needed to get 28-02-2016
, so as to keep it within next month. In such cases you can use the solution below.
对于我的情况,我需要得到28-02-2016
,以便在下个月内保留它。在这种情况下,您可以使用以下解决方案。
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
回答by Michaela.Merz
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). 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.
这将计算从现在开始的确切日期 n*count 个月(其中 count <= 12)。如果该服务于 2019 年 3 月 31 日开始并运行 11 个月,则将于 2020 年 2 月 29 日结束。如果仅运行 1 个月,则结束日期为 2019 年 4 月 30 日。
回答by drooh
$nextm = date('m', strtotime('+1 month', strtotime(date('Y-m-01'))));
$nextm = date('m', strtotime('+1 月', strtotime(date('Ym-01'))));