dtrgqjcd877528 2016-10-24 16:38
浏览 52
已采纳

如果date1通过,PHP显示date2

I need help on this..
I am new in php and dummy..

$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"   

please help with the code...
Thanks..

IM php DUMMY

  • 写回答

1条回答 默认 最新

  • dongzichan2886 2016-10-24 20:15
    关注

    Organize your data convenient so you can easy access it. Put your paydates it an array:

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

    UPDATED per your comment:

    $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];
    

    Transform the dates in an easy to sort format:

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

    Sort the array to be sure we will get the dates in ascending order while looping.

    sort($paydate);
    

    Now loop through the array to find the first date higher then today:

    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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀