假设我的日期格式如下:2010-12-11(year-mon-day)
使用PHP,我希望将日期增加一个月,如果需要,我希望年份自动递增(即从2012年12月到2013年1月递增)。
问候。
答案 0 :(得分:140)
$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));
// Finally you will have the date you're looking for.
答案 1 :(得分:30)
我需要类似的功能,除了每月周期(加上月,减去1天)。搜索S.O.有一段时间,我能够制作这种即插即用的解决方案:
function add_months($months, DateTime $dateObject)
{
$next = new DateTime($dateObject->format('Y-m-d'));
$next->modify('last day of +'.$months.' month');
if($dateObject->format('d') > $next->format('d')) {
return $dateObject->diff($next);
} else {
return new DateInterval('P'.$months.'M');
}
}
function endCycle($d1, $months)
{
$date = new DateTime($d1);
// call second function to add the months
$newDate = $date->add(add_months($months, $date));
// goes back 1 day from date, remove if you want same day of month
$newDate->sub(new DateInterval('P1D'));
//formats final date to Y-m-d form
$dateReturned = $newDate->format('Y-m-d');
return $dateReturned;
}
示例:
$startDate = '2014-06-03'; // select date in Y-m-d format
$nMonths = 1; // choose how many months you want to move ahead
$final = endCycle($startDate, $nMonths); // output: 2014-07-02
答案 2 :(得分:26)
$start = new DateTime("2010-12-11", new DateTimeZone("UTC"));
$month_later = clone $start;
$month_later->add(new DateInterval("P1M"));
我使用了clone,因为add会修改原始对象,这可能是不可取的。
答案 3 :(得分:13)
strtotime( "+1 month", strtotime( $time ) );
这将返回一个可与日期函数
一起使用的时间戳答案 4 :(得分:5)
我用这种方式: -
$occDate='2014-01-28';
$forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=02
/*****************more example****************/
$occDate='2014-12-28';
$forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=01
//***********************wrong way**********************************//
$forOdNextMonth= date('m', strtotime("+1 month", $occDate));
//Output:- $forOdNextMonth=02; //instead of $forOdNextMonth=01;
//******************************************************************//
答案 5 :(得分:5)
您可以像这样使用DateTime::modify
:
$date = new DateTime('2010-12-11');
$date->modify('+1 month');
见文件:
答案 6 :(得分:4)
(date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));
这将弥补二月和其他31天的月份。你当然可以做更多的检查,以便在下个月的这一天得到更准确的信息。 relative date formats(遗憾地工作,见下文),你也可以使用DateTime。
无论下个月的天数如何,DateInterval('P1M')
和strtotime("+1 month")
基本上都会盲目地添加31天。
答案 7 :(得分:3)
请先将日期格式设置为12-12-2012
使用此功能后,它正常工作;
$date = date('d-m-Y',strtotime("12-12-2012 +2 Months");
此处12-12-2012是您的日期,+ 2个月是月份的增量;
您还可以增加年份,日期
strtotime("12-12-2012 +1 Year");
Ans是12-12-2013
答案 8 :(得分:1)
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+1 month", $date));
如果你想增加天数,你也可以这样做
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+5 day", $date));
答案 9 :(得分:0)
function dayOfWeek($date){
return DateTime::createFromFormat('Y-m-d', $date)->format('N');
}
用法示例:
echo dayOfWeek(2016-12-22);
// "4"
echo dayOfWeek(date('Y-m-d'));
// "4"
答案 10 :(得分:0)
任何寻找任何日期格式答案的人。
echo date_create_from_format('d/m/Y', '15/04/2017')->add(new DateInterval('P1M'))->format('d/m/Y');
只需更改日期格式。
答案 11 :(得分:0)
谢谢杰森,你的帖子非常有帮助。我重新格式化了它并添加了更多评论以帮助我理解这一切。如果有人帮助我,我已在此处发布:
function cycle_end_date($cycle_start_date, $months) {
$cycle_start_date_object = new DateTime($cycle_start_date);
//Find the date interval that we will need to add to the start date
$date_interval = find_date_interval($months, $cycle_start_date_object);
//Add this date interval to the current date (the DateTime class handles remaining complexity like year-ends)
$cycle_end_date_object = $cycle_start_date_object->add($date_interval);
//Subtract (sub) 1 day from date
$cycle_end_date_object->sub(new DateInterval('P1D'));
//Format final date to Y-m-d
$cycle_end_date = $cycle_end_date_object->format('Y-m-d');
return $cycle_end_date;
}
//Find the date interval we need to add to start date to get end date
function find_date_interval($n_months, DateTime $cycle_start_date_object) {
//Create new datetime object identical to inputted one
$date_of_last_day_next_month = new DateTime($cycle_start_date_object->format('Y-m-d'));
//And modify it so it is the date of the last day of the next month
$date_of_last_day_next_month->modify('last day of +'.$n_months.' month');
//If the day of inputted date (e.g. 31) is greater than last day of next month (e.g. 28)
if($cycle_start_date_object->format('d') > $date_of_last_day_next_month->format('d')) {
//Return a DateInterval object equal to the number of days difference
return $cycle_start_date_object->diff($date_of_last_day_next_month);
//Otherwise the date is easy and we can just add a month to it
} else {
//Return a DateInterval object equal to a period (P) of 1 month (M)
return new DateInterval('P'.$n_months.'M');
}
}
$cycle_start_date = '2014-01-31'; // select date in Y-m-d format
$n_months = 1; // choose how many months you want to move ahead
$cycle_end_date = cycle_end_date($cycle_start_date, $n_months); // output: 2014-07-02
答案 12 :(得分:0)
所有提出的解决方案无法正常工作。
strtotime()和DateTime::add或DateTime::modify有时会给出无效的结果。
例子:
-31.08.2019 + 1个月为01.10.2019而不是30.09.2019
-29.02.2020 + 1年给出01.03.2021而不是28.02.2021
(已在PHP 5.5,PHP 7.3上测试)
以下是我基于idea posted by Angelo的函数来解决的问题:
// $time - unix time or date in any format accepted by strtotime() e.g. 2020-02-29
// $days, $months, $years - values to add
// returns new date in format 2021-02-28
function addTime($time, $days, $months, $years)
{
// Convert unix time to date format
if (is_numeric($time))
$time = date('Y-m-d', $time);
try
{
$date_time = new DateTime($time);
}
catch (Exception $e)
{
echo $e->getMessage();
exit;
}
if ($days)
$date_time->add(new DateInterval('P'.$days.'D'));
// Preserve day number
if ($months or $years)
$old_day = $date_time->format('d');
if ($months)
$date_time->add(new DateInterval('P'.$months.'M'));
if ($years)
$date_time->add(new DateInterval('P'.$years.'Y'));
// Patch for adding months or years
if ($months or $years)
{
$new_day = $date_time->format("d");
// The day is changed - set the last day of the previous month
if ($old_day != $new_day)
$date_time->sub(new DateInterval('P'.$new_day.'D'));
}
// You can chage returned format here
return $date_time->format('Y-m-d');
}
用法示例:
echo addTime('2020-02-29', 0, 0, 1); // add 1 year (result: 2021-02-28)
echo addTime('2019-08-31', 0, 1, 0); // add 1 month (result: 2019-09-30)
echo addTime('2019-03-15', 12, 2, 1); // add 12 days, 2 months, 1 year (result: 2019-09-30)
答案 13 :(得分:0)
只需使用简单的方法更新答案即可找到几个月后的日期。由于标记为最佳答案并不能给出正确的解决方案。
let theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.layer.borderWidth = 2.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
答案 14 :(得分:0)
如果您想从现在开始获取一个月的日期,可以这样
echo date('Y-m-d', strtotime('1 month'));
如果您希望从现在起获得两个月的日期,可以通过执行此操作
echo date('Y-m-d', strtotime('2 month'));
等等,仅此而已。
答案 15 :(得分:0)
//ECHO MONTHS BETWEEN TWO TIMESTAMPS
$my_earliest_timestamp = 1532095200;
$my_latest_timestamp = 1554991200;
echo '<pre>';
echo "Earliest timestamp: ". date('c',$my_earliest_timestamp) ."\r\n";
echo "Latest timestamp: " .date('c',$my_latest_timestamp) ."\r\n\r\n";
echo "Month start of earliest timestamp: ". date('c',strtotime('first day of '. date('F Y',$my_earliest_timestamp))) ."\r\n";
echo "Month start of latest timestamp: " .date('c',strtotime('first day of '. date('F Y',$my_latest_timestamp))) ."\r\n\r\n";
echo "Month end of earliest timestamp: ". date('c',strtotime('last day of '. date('F Y',$my_earliest_timestamp)) + 86399) ."\r\n";
echo "Month end of latest timestamp: " .date('c',strtotime('last day of '. date('F Y',$my_latest_timestamp)) + 86399) ."\r\n\r\n";
$sMonth = strtotime('first day of '. date('F Y',$my_earliest_timestamp));
$eMonth = strtotime('last day of '. date('F Y',$my_earliest_timestamp)) + 86399;
$xMonth = strtotime('+1 month', strtotime('first day of '. date('F Y',$my_latest_timestamp)));
while ($eMonth < $xMonth) {
echo "Things from ". date('Y-m-d',$sMonth) ." to ". date('Y-m-d',$eMonth) ."\r\n\r\n";
$sMonth = $eMonth + 1; //add 1 second to bring forward last date into first second of next month.
$eMonth = strtotime('last day of '. date('F Y',$sMonth)) + 86399;
}
答案 16 :(得分:-1)
在输入框中输入日期,然后点击日期在jquery
中的按钮获取日期$(document).ready( function() {
$("button").click(function(){
var day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var a = new Date();
$(".result").text(day[a.getDay()]);
});
});
答案 17 :(得分:-1)
<?php
$selectdata ="select fromd,tod from register where username='$username'";
$q=mysqli_query($conm,$selectdata);
$row=mysqli_fetch_array($q);
$startdate=$row['fromd'];
$stdate=date('Y', strtotime($startdate));
$endate=$row['tod'];
$enddate=date('Y', strtotime($endate));
$years = range ($stdate,$enddate);
echo '<select name="years" class="form-control">';
echo '<option>SELECT</option>';
foreach($years as $year)
{ echo '<option value="'.$year.'"> '.$year.' </option>'; }
echo '</select>'; ?>