php获得6个月以前的日期,PHP 获取某个日期前N个月或者N年的日期,如2016-02-29的前一个月为2016-01-31...

/**

* Created by PhpStorm.

* User: Morpheus.Lan

* Date: 2017/12/18

* Time: 11:17

*/

error_reporting(0);

function mod($year,$i){

return $year%$i;

}

function isRunYear($year){

if(mod($year,100)==0){

if(mod($year,400)==0){

return true;

}else{

return false;

}

}else{

if(mod($year,4)==0){

return true;

}else{

return false;

}

}

}

function getNewDate($date,$month_num=0,$year_num=0){

$date_arr = explode("-",$date);

$new_year = (int)$date_arr[0]-$year_num;

$new_month=0;

$new_day=0;

$month_limit = mod($month_num,12);

if($month_num>=12){

$year_add=intval(floor($month_num/12));

if($month_limit>=(int)$date_arr[1]){

$new_year = $new_year-($year_add+1);

$new_month = 12+(int)$date_arr[1]-$month_limit;

}else{

$new_year = $new_year-$year_add;

$new_month = (int)$date_arr[1]-$month_limit;

}

}else{

if($month_limit>=(int)$date_arr[1]){

$new_year = $new_year-1;

$new_month = 12+(int)$date_arr[1]-$month_limit;

}else{

$new_month = (int)$date_arr[1]-$month_limit;

}

}

if((int)$date_arr[1]==2){

if(isRunYear($date_arr[0])){

if((int)$date_arr[2]==29){

if(in_array($new_month,array(1,3,5,7,8,10,12))){

$new_day = 31;

}

if(in_array($new_month,array(4,6,9,11))){

$new_day = 30;

}

if($new_month==2){

if(isRunYear($new_year)){

$new_day = 29;

}else{

$new_day = 28;

}

}

}else{

$new_day = $date_arr[2];

}

}else{

if((int)$date_arr[2]==28){

if(in_array($new_month,array(1,3,5,7,8,10,12))){

$new_day = 31;

}

if(in_array($new_month,array(4,6,9,11))){

$new_day = 30;

}

if($new_month==2){

if(isRunYear($new_year)){

$new_day = 29;

}else{

$new_day = 28;

}

}

}else{

$new_day = $date_arr[2];

}

}

}

if(in_array((int)$date_arr[1],array(1,3,5,7,8,10,12))){

if((int)$date_arr[2]==31){

if(in_array($new_month,array(1,3,5,7,8,10,12))){

$new_day = 31;

}

if(in_array($new_month,array(4,6,9,11))){

$new_day = 30;

}

if($new_month==2){

if(isRunYear($new_year)){

$new_day = 29;

}else{

$new_day = 28;

}

}

}else{

$new_day = $date_arr[2];

if(($new_month==2)&&($new_day>28)){

if(isRunYear($new_year)){

$new_day = 29;

}else{

$new_day = 28;

}

}

}

}

if(in_array((int)$date_arr[1],array(4,6,9,11))){

if((int)$date_arr[2]==30){

if(in_array($new_month,array(1,3,5,7,8,10,12))){

$new_day = 31;

}

if(in_array($new_month,array(4,6,9,11))){

$new_day = 30;

}

if($new_month==2){

if(isRunYear($new_year)){

$new_day = 29;

}else{

$new_day = 28;

}

}

}else{

$new_day = $date_arr[2];

if(($new_month==2)&&($new_day>28)){

if(isRunYear($new_year)){

$new_day = 29;

}else{

$new_day = 28;

}

}

}

}

if($new_month<10){

$new_month="0".$new_month;

}

return $new_year."-".$new_month."-".$new_day;

}

//

$date = $_GET['date'];

$year_num = $_GET['year_num']?$_GET['year_num']:0;

$month_num = $_GET['month_num']?$_GET['month_num']:0;

$return = getNewDate($date,$month_num);

echo $return;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值