如果date1通过,PHP显示date2

时间:2016-10-24 16:38:11

标签: php

我需要帮助..
我是php和虚拟的新手..

$displaydate = "";

$paydate1 = "23-10-2016";
$paydate2 = "23-11-2016";   
$paydate3 = "23-12-2016";    
$paydate4 = "23-01-2017";    
$paydate5 = "23-02-2017";   
$paydate6 = "23-03-2017";




if todaydate is 23-10-2016 then $displaydate = "$paydate2" until 22-11-2016. When 23-11-2016 then $displaydate = "$paydate3" and so on.



then results    
if the date is 23-10-2016 until 22-11-2016 $displaydate = "$paydate2"   
if the date is 23-11-2016 until 22-12-2016 $displaydate = "$paydate3"    
if the date is 23-12-2016 until 22-01-2016 $displaydate = "$paydate4"   
if the date is 23-01-2016 until 22-02-2016 $displaydate = "$paydate5"  
if the date is 23-02-2016 until 22-03-2016 $displaydate = "$paydate6"   

请帮助代码......
感谢..

IM php DUMMY

1 个答案:

答案 0 :(得分:1)

方便地整理您的数据,以便您轻松访问它。 把你的paydates放在一个数组:

$paydate = ["23-10-2016","23-11-2016","23-12-2016","23-01-2017","23-02-2017","23-03-2017"];

根据您的评论更新:

$payout1 = $this->data->paymentdate1; 
$payout2 = $this->data->paymentdate2; 
$payout3 = $this->data->paymentdate3; 
$payout4 = $this->data->paymentdate4; 
$payout5 = $this->data->paymentdate5; 
$payout6 = $this->data->paymentdate6; 

$paydate = [$payout1,$payout2,$payout3,$payout4,$payout5,$pa‌​yout6];

以易于排序的格式转换日期:

function trf_date($date)
{
    return date('Y-m-d', strtotime($date));
}
$paydate = array_map("trf_date", $paydate );

对数组进行排序,以确保在循环时我们将按升序获取日期。

sort($paydate);

现在遍历数组以找到比今天更高的第一个日期:

foreach($paydate as $key=>$val){
   if($val > date('Y-m-d'))
     break;
}

$displaydate = ''; //initialize the output variable

//check to see if the last retrieved date meets the condition not to be in the past. 
//this is for the case all dates in the array are in the past
if(isset($paydate[$key]) && $paydate[$key] > date('Y-m-d'))
   $displaydate = $paydate[$key];