这是我的代码,我在其中根据当前日期计算到期日。如果用户在2月2日轮班,那么根据当前日期,到期日期将显示为6月1日。
$current_date=date('Y-m-d');
$ref_date=date('Y-m-d', strtotime($ref_date));
if($current_date>$ref_date){
$ref_month=date('m', strtotime($ref_date));
if($ref_month=="12"){
$ref_new_year = strtotime("1 year", strtotime($ref_date));
$due_year=date("Y", $ref_new_year);
}else{
$due_year=date('Y', strtotime($ref_date));
}
$due_day=date('d', strtotime($ref_date));
$due_month=date('m', strtotime($ref_date));
$check_year=$due_year."-".$due_month."-".$due_day;
$shift_month= date("m", strtotime($ref_date));
$current_month=date("m", strtotime($current_date));
$my_month1=$current_month-$shift_month;
$my_month=$my_month1+1;
$your_date1 = strtotime("$my_month month", strtotime($ref_date));
$due_month1= date("m", $your_date1);
$due_month121= date("M", $your_date1);
$due_day1=date('d', strtotime($check_year));
$check_month=$due_year."-".$due_month1."-".$due_day1;
$ref_date12 = strtotime("-1 day", strtotime($check_month));
$due_date11= date("d", $ref_date12);
$ref_final_date=$due_year."-".$due_month1."-".$due_date11;
$ref_date11=date('d', strtotime($ref_final_date));
$current_date=date("d", strtotime($current_date));
if($current_date<$ref_date11){
$your_date1321 = strtotime("-1 month", strtotime($ref_final_date));
$due_month22= date("M", $your_date1321);
echo $final_date=$due_date11."-".$due_month22."-".$due_year;
}else{
echo $final_date=$due_date11."-".$due_month121."-".$due_year;
}
}else{
//Year
$ref_month=date('m', strtotime($ref_date));
if($ref_month=="12"){
$ref_new_year = strtotime("1 year", strtotime($ref_date));
$due_year=date("Y", $ref_new_year);
}else{
$due_year=date('Y', strtotime($ref_date));
}
$due_day=date('d', strtotime($ref_date));
$due_month=date('m', strtotime($ref_date));
$check_year=$due_year."-".$due_month."-".$due_day;
//Month
$ref_month = strtotime("1 month", strtotime($check_year));
$due_month1= date("m", $ref_month);
$due_month1321= date("M", $ref_month);
$due_day1=date('d', strtotime($check_year));
$check_month=$due_year."-".$due_month1."-".$due_day1;
//Date
$ref_date12 = strtotime("-1 day", strtotime($check_month));
$due_date11= date("d", $ref_date12);
if($current_date < $ref_date){
echo "Tenant not shifted yet.";
}else{
echo $final_date=$due_date11."-".$due_month1321."-".$due_year;
}
}在这段代码中,我面临的问题是,如果用户的轮班日期是3月1日,那么到期日是6月31日,但6月只有30天。请帮帮我。
发布于 2018-05-20 19:19:28
您正在使用日期函数,这很有趣,因为php确实认为6月31日不存在。
如果您可以对您的代码进行评论,我们可以提供进一步的帮助
如果你想在长途上做,你可以做-1天如果4个月,6个月等(30天月)
发布于 2018-05-20 23:59:53
使用mktime()函数根据当前日期/时间构建未来日期。在调用函数时,只需在days参数中加上30即可。
请注意,我之所以使用intval(),是因为没有一个date()格式的字符串的这些日期部分没有前导0,这让一些版本的PHP混淆了它的“整型”。如果您所关心的只是日期而不是时间,那么可以随意硬编码一个像1这样的值,而不是使用date()调用来获取小时/分钟/秒部分。
<?php
$hours=date("G");
$minutes=intval(date("i"));
$seconds=intval(date("s"));
$month=date("n");
$day=date("j");
$year=date("Y");
$today=mktime($hours,$minutes,$seconds,$month,$day,$year);
$futureDate=mktime($hours,$minutes,$seconds,$month,($day+30),$year);
print("Today is ".date("n-j-Y",$today)."<br />\n");
print("in 30 days it will be ".date("n-j-Y",$futureDate)."<br />\n");
?>https://stackoverflow.com/questions/50433917
复制相似问题