PHP添加1个月至今
我有一个函数,返回1个月前的url 我想显示当前所选月份,但我不能使用简单的当前月份,因为当用户单击指向1个月后的链接时,所选月份将更改,并且不会是当前月份 因此,函数返回2012年8月 我如何制作一个小php脚本来增加一个月的时间 到目前为止,我已经:PHP添加1个月至今,php,date,Php,Date,我有一个函数,返回1个月前的url 我想显示当前所选月份,但我不能使用简单的当前月份,因为当用户单击指向1个月后的链接时,所选月份将更改,并且不会是当前月份 因此,函数返回2012年8月 我如何制作一个小php脚本来增加一个月的时间 到目前为止,我已经: <?php echo strip_tags(tribe_get_previous_month_text()); ?> 您可以使用DateTime类和DateTime::add()方法: 简单方法: $next_month = st
<?php echo strip_tags(tribe_get_previous_month_text()); ?>
您可以使用DateTime类和DateTime::add()方法: 简单方法:
$next_month = strtotime('august 2012 next month');
更好的方法:
$d = new Date('August 2012');
$next_month = $d->add(new DateInterval('P1M'));
相关文档:有3个选项/答案
$givendate is the given date (ex. 2016-01-20)
option 1:
$date1 = date('Y-m-d', strtotime($givendate. ' + 1 month'));
option 2:
$date2 = date('Y-m-d', strtotime($givendate. ' + 30 days'));
option 3:
$number = cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($givendate)), date('Y', strtotime($givendate)));
$date3 = date('Y-m-d', strtotime($date2. ' + '.$number.' days'));
您可以简单地对2012年4月必须到达的任何输入使用
strotime
功能,然后应用date
和strotime
,增量周期为“+1个月”
$x = strtotime($t);
$n = date("M Y",strtotime("+1 month",$x));
echo $n;
以下是PHP手册中的相关章节:
此解决方案解决了将任意时间量增加到时间值的附加问题 除了他们的答案,大家好。我想如果你只是想根据当前日期得到下个月的数据,我的解决方案是这样的
$today = date("Y-m-01");
$sNextMonth = (int)date("m",strtotime($today." +1 months") );
请注意,我经常将日期定义为01,这样我们就可以安全地获得下一个月。如果是日期(“Y-m-d”);而今天是31,它将失败
希望这有帮助。日期差异
$date1 = '2017-01-20';
$date2 = '2019-01-20';
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
echo $joining_months = (($year2 - $year1) * 12) + ($month2 - $month1);
因为我们知道strotime(+1个月)通常会增加30天,如果日期以31、30或29天结束,并且您仍然希望停留在下个月的最后一天内,则可能会遇到一些问题 因此,我编写了这个复杂的脚本来解决这个问题,并进行了调整,以便您可以增加所有类型的格式,如年、月、日、小时、分钟和秒
function seetime($datetime, $p = '+', $i, $m = 'M', $f = 'Y-m-d H:i:s')
{
/*
$datetime needs to be in format of YYYY-MM-DD HH:II:SS but hours, minutes and seconds are not required
$p can only be "+" to increse or "-" to decrese
$i is the amount you want to change
$m is the type you want to change
Allowed types:
Y = Year
M = Months
D = Days
W = Weeks
H = Hours
I = Minutes
S = Seconds
$f is the datetime format you want the result to be returned in
*/
$validator_y = substr($datetime,0,4);
$validator_m = substr($datetime,5,2);
$validator_d = substr($datetime,8,2);
if(checkdate($validator_m, $validator_d, $validator_y))
{
$datetime = date('Y-m-d H:i:s', strtotime($datetime));
#$p = either "+" to add or "-" to subtract
if($p == '+' || $p == '-')
{
if(is_int($i))
{
if($m == 'Y')
{
$year = date('Y', strtotime($datetime));
$rest = date('m-d H:i:s', strtotime($datetime));
if($p == '+')
{
$ret = $year + $i;
}
else
{
$ret = $year - $i;
}
$str = $ret.'-'.$rest;
return(date($f, strtotime($str)));
}
elseif($m == 'M')
{
$year = date('Y', strtotime($datetime));
$month = date('n', strtotime($datetime));
$rest = date('d H:i:s', strtotime($datetime));
$his = date('H:i:s', strtotime($datetime));
if($p == '+')
{
$ret = $month + $i;
$ret = sprintf("%02d",$ret);
}
else
{
$ret = $month - $i;
$ret = sprintf("%02d",$ret);
}
if($ret < 1)
{
$ret = $ret - $ret - $ret;
$years_back = floor(($ret + 12) / 12);
$monts_back = $ret % 12;
$year = $year - $years_back;
$month = 12 - $monts_back;
$month = sprintf("%02d",$month);
$new_date = $year.'-'.$month.'-'.$rest;
$ym = $year.'-'.$month;
$validator_y = substr($new_date,0,4);
$validator_m = substr($new_date,5,2);
$validator_d = substr($new_date,8,2);
if(checkdate($validator_m, $validator_d, $validator_y))
{
return (date($f, strtotime($new_date)));
}
else
{
$days = date('t',strtotime($ym));
$new_date = $ym.'-'.$days.' '.$his;
return (date($f, strtotime($new_date)));
}
}
if($ret > 12)
{
$years_forw = floor($ret / 12);
$monts_forw = $ret % 12;
$year = $year + $years_forw;
$month = sprintf("%02d",$monts_forw);
$new_date = $year.'-'.$month.'-'.$rest;
$ym = $year.'-'.$month;
$validator_y = substr($new_date,0,4);
$validator_m = substr($new_date,5,2);
$validator_d = substr($new_date,8,2);
if(checkdate($validator_m, $validator_d, $validator_y))
{
return (date($f, strtotime($new_date)));
}
else
{
$days = date('t',strtotime($ym));
$new_date = $ym.'-'.$days.' '.$his;
return (date($f, strtotime($new_date)));
}
}
else
{
$ym = $year.'-'.$month;
$new_date = $year.'-'.$ret.'-'.$rest;
$validator_y = substr($new_date,0,4);
$validator_m = substr($new_date,5,2);
$validator_d = substr($new_date,8,2);
if(checkdate($validator_m, $validator_d, $validator_y))
{
return (date($f, strtotime($new_date)));
}
else
{
$ym = $validator_y . '-'.$validator_m;
$days = date('t',strtotime($ym));
$new_date = $ym.'-'.$days.' '.$his;
return (date($f, strtotime($new_date)));
}
}
}
elseif($m == 'D')
{
return (date($f, strtotime($datetime.' '.$p.$i.' days')));
}
elseif($m == 'W')
{
return (date($f, strtotime($datetime.' '.$p.$i.' weeks')));
}
elseif($m == 'H')
{
return (date($f, strtotime($datetime.' '.$p.$i.' hours')));
}
elseif($m == 'I')
{
return (date($f, strtotime($datetime.' '.$p.$i.' minutes')));
}
elseif($m == 'S')
{
return (date($f, strtotime($datetime.' '.$p.$i.' seconds')));
}
else
{
return 'Fourth parameter can only be any of following: Valid Time Parameters Are: Y M D Q H I S';
}
}
else
{
return 'Third parameter can only be a number (whole number)';
}
}
else
{
return 'Second parameter can only be + to add or - to subtract';
}
}
else
{
return 'Date is not a valid date';
}
}
函数seetime($datetime,$p='+',$i,$m='m',$f='Y-m-dh:i:s'))
{
/*
$datetime需要采用YYYY-MM-DD HH:II:SS格式,但不需要小时、分钟和秒
$p只能为“+”递增,或为“-”递减
$i是您要更改的金额
$m是您要更改的类型
允许的类型:
Y=年
M=月
D=天
W=周
H=小时
I=分钟
S=秒
$f是您希望返回结果的日期时间格式
*/
$validator_y=substr($datetime,0,4);
$validator_m=substr($datetime,5,2);
$validator_d=substr($datetime,8,2);
if(检查日期($validator\u m、$validator\u d、$validator\u y))
{
$datetime=date('Y-m-dh:i:s',strottime($datetime));
#$p=加“+”或减“-”
如果($p='+'| |$p=='-'))
{
if(is_int($i))
{
如果($m='Y')
{
$year=date('Y',strottime($datetime));
$rest=date('m-dh:i:s',strottime($datetime));
如果($p='+'))
{
$ret=$year+$i;
}
其他的
{
$ret=$year-$i;
}
$str=$ret.-'.$rest;
申报表(日期($f,标准时间($str));
}
elseif($m='m')
{
$year=date('Y',strottime($datetime));
$month=date('n',strottime($datetime));
$rest=date('dh:i:s',strottime($datetime));
$his=日期('H:i:s',strottime($datetime));
如果($p='+'))
{
$ret=$month+$i;
$ret=sprintf(“%02d”,$ret);
}
其他的
{
$ret=$month-$i;
$ret=sprintf(“%02d”,$ret);
}
如果($ret<1)
{
$ret=$ret-$ret-$ret;
$years\u back=地板($ret+12)/12);
$monts_back=$ret%12;
$year=$year-$years\u back;
$month=12-$monts_back;
$month=sprintf(“%02d”,$month);
$new_date=$year.'-'.$month.'-'.$rest;
$ym=$year.'-'.$month;
$validator_y=substr($new_date,0,4);
$validator_m=substr($new_date,5,2);
$validator_d=substr($new_date,8,2);
if(检查日期($validator\u m、$validator\u d、$validator\u y))
{
返回(日期($f,标准时间($new_日期));
}
其他的
{
$days=日期('t',strottime($ym));
$new_date=$ym.'-'.$days.'.$his;
返回(日期($f,标准时间($new_日期));
}
}
如果($ret>12)
{
$years\u forw=地板($ret/12);
$monts_forw=$ret%12;
$year=$year+$years\u forw;
$month=sprintf(“%02d”,每月$monts_);
$new_date=$year.'-'.$month.'-'.$rest;
$ym=$year.'-'.$month;
$validator_y=substr($new_date,0,4);
$validator_m=substr($new_date,5,2);
$validator_d=substr($new_date,8,2);
if(检查日期($validator\u m、$validator\u d、$validator\u y))
{
返回(日期($f,标准时间($new_日期));